Skip to Content
LINUX系統程式設計 第二版
book

LINUX系統程式設計 第二版

by Robert Love
December 2013
Intermediate to advanced
496 pages
8h 57m
Chinese
GoTop Information, Inc.
Content preview from LINUX系統程式設計 第二版
執行緒
|
249
ESRCH
thread
指定了無效的執行緒。
下面是使用範例:
int ret;
/* 連接 `thread' 而且我們並不在乎它的傳回值 */
ret = pthread_join (thread, NULL);
if (ret) {
errno = ret;
perror ("pthread_join");
return -1;
}
分離執行緒
預設情況下,執行緒都是
可連接的
joinable
)。然而,你可以
分離
detach
)執行緒,
使其不再可連接。因為執行緒會耗用系統資源直到被連接為止,正如行程會耗用系統資
源直到其父行程叫用
wait()
,你不打算連接的執行緒應該予以分離。
#include <pthread.h>
int pthread_detach (pthread_t thread);
調用成功時,
pthread_detach()
會分離
thread
所指定的執行緒,並傳回零值。如果被你
調用
pthread_detach()
的執行緒早已被分離,則結果未定義。發生錯誤時,此函式會傳
ESRCH
,代表
thread
是無效的。
你應該對行程中的每個執行緒調用
pthread_join()
pthread_detach()
,以便在執行緒
終止時釋出系統資源。(當然,如果整個行程都結束了,那麼所有執行緒的資源都會被
釋出,不過明確連接或分離所有執行緒仍是好的做法。)
執行緒範例程式
接著讓我們來看一支完整的範例程式,其中用到了目前為止我們討論過的介面。它會
建立 2 個執行緒(總共有 3 個),在同一個起始常式 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

優雅的SciPy|Python科學研究的美學

優雅的SciPy|Python科學研究的美學

Juan Nunez-Iglesias, Stéfan van der Walt, Harriet Dashnow
C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普

Publisher Resources

ISBN: 9789862769812