《Linux多線程編程手冊》讀書筆記

第二章 基本線程編程編程

1.(P25)若是多個線程等待同一個線程終止,則全部等待線程將一直等到目標線程終止。而後,一個等待線程成功返回,其他的等待線程將失敗並返回ESRCH錯誤函數

2.(P26)將新線程的pbe參數做爲棧參數進行傳遞。這個線程參數之因此可以做爲棧參數傳遞,是由於主線程會等待輔助線程終止。不過,首選方法是使用malloc從堆分配存儲,而不是傳遞指向線程棧存儲的地址。若是將該參數做爲地址傳遞到線程棧存儲,則該地址可能無效或者在線程終止時會被從新分配。spa

3.(P28)pthread_detach(3C)是pthread_join()的替代函數,可回收建立時detachstate屬性設置爲PTHREAD_CREATE_JOINABLE的線程的存儲空間。線程

int pthread_detach(thread_t tid);

4.(P36)若是tid1和tid2相等,pthread_equal()將返回非零值,不然將返回零。若是tid1或tid2是無效的線程標識號,則結果沒法預測。code

int pthread_equal(pthread_t tid1, pthread_t tid2);

5.(P37)使用sched_yield(3RT),可使當前線程中止執行,以便執行另外一個具備相同或更高優先級的線程。blog

int sched_yield(void);

6.(P38)請使用pthread_setschedparam()修改現有線程的優先級。此函數對於調度策略不起做用。pthread_getschedparam(3C)可用來獲取現有線程的優先級。進程

int pthread_setschedparam(pthread_t tid, int policy,const struct sched_param *param);
int pthread_getschedparam(pthread_t tid, int policy,struct schedparam *param);
#include <pthread.h>
pthread_t tid; int ret; struct sched_param param; int priority;
/* sched_priority will be the priority of the thread */ param.sched_priority = priority; policy= SCHED_OTHER;
/* scheduling parameters of target thread */ ret = pthread_setschedparam(tid,policy,&param);

7.(P40)請使用pthread_kill()向線程發送信號。sig參數必須來自signal(5)提供的列表。若是sig爲零,將執行錯誤檢查,但並不實際發送信號。此錯誤檢查可用來檢查tid的有效性。get

int pthread_kill(thread_t tid, int sig);

8.(P43)當初始線程(即調用main()的線程)從main()調用返回時或調用exit()時,整個進程及其全部的線程將終止。若是主線程僅僅調用了pthread_exit,則僅主線程自己終止。進程及進程內的其餘線程將繼續存在。全部線程都已終止時,進程也將終止。it

相關文章
相關標籤/搜索