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系統程式設計 第二版
248
|
第七章
接著,由不同的執行緒送出取消請求:
int ret;
/* `thread' 是要被終止之執行緒的執行緒識別碼 */
ret = pthread_cancel (thread);
if (ret) {
errno = ret;
perror ("pthread_cancel");
return -1;
}
連接及分離執行緒
由於執行緒可以輕易被建立及銷毀,必須透過某種方式來同步執行緒與被終止的其他執
行緒—也就是,
wait()
的執行緒版本。的確有:就是連接執行緒。
連接執行緒
連接
Joining
)的功能讓一個執行緒在等待另一個終止執行緒的時候遭到阻擋:
#include <pthread.h>
int pthread_join (pthread_t thread, void **retval);
調用成功時,進行調用的執行緒會遭到阻擋,直到
thread
所指定的執行緒終止執行(如
thread
已經終止執行,
pthread_join()
會立即返回)。一旦
thread
終止執行,進行
調用的行程會被喚醒,如果
retval
的值不是
NULL
,其所提供的是被終止執行緒傳遞給
pthread_exit()
的傳回值,或是起始常式的傳回值。然後我們會說執行緒被連接在一起
了。連接功能讓執行緒的執行得以跟其他執行緒的生命週期同步。Pthread 中所有執行緒
都是對等的;任何執行緒都可以連接任何其他執行緒。單一執行緒可以連接許多執行緒
(事實上,正如我們將看到的,這往往是主要執行緒等待其所建立之執行緒的方式),
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