Posted on 2009-11-18 17:18
Prayer 閱讀(943)
評論(0) 編輯 收藏 引用 所屬分類:
LINUX/UNIX/AIX
線程基本編程
線程基本編程
索引:
1.創(chuàng)建線程pthread_create
2.等待線程結(jié)束pthread_join
3.分離線程pthread_detach
4.創(chuàng)建線程鍵pthread_key_create
5.刪除線程鍵pthread_key_delete
6.設(shè)置線程數(shù)據(jù)pthread_setspecific
7.獲取線程數(shù)據(jù)pthread_getspecific
8.獲取線程標示符pthread_self
9.比較線程pthread_equal
10.一次執(zhí)行pthread_once
11.出讓執(zhí)行權(quán)sched_yield
12.修改優(yōu)先級pthread_setschedparam
13.獲取優(yōu)先級pthread_getschedparam
14.發(fā)送信號pthread_kill
15.設(shè)置線程掩碼pthread_sigmask
16.終止線程pthread_exit
17.退出線程pthread_cancel
18.允許/禁止退出線程pthread_setcancelstate
19.設(shè)置退出類型pthread_setcanceltype
20.創(chuàng)建退出點pthread_testcancel
21.壓入善后處理函數(shù)
22.彈出善后處理函數(shù)
--------------------------------------------------------------------------------
1.創(chuàng)建線程pthread_create
#include
int pthread_create(pthread_t *tid, const pthread_attr_t *tattr, void *(*start_routine)(void *), void *arg);
返回值:函數(shù)成功返回0。任何其他返回值都表示錯誤。
創(chuàng)建一個線程。
參數(shù)tattr中含有初始化線程所需要的屬性,start_routine是線程入口函數(shù)的地址,當start_routine返回時,相應的線程就結(jié)束了。
當函數(shù)成功時,線程標示符保存在參數(shù)tid指向的內(nèi)存中。
如果不指定屬性對象,將其置為NULL,則創(chuàng)建一個缺省的線程,有如下屬性:
非綁定的;
未分離的;
由一個缺省大小的堆棧;
具有和父線程一樣的優(yōu)先級。
注意:在創(chuàng)建子線程時,傳給子線程的輸入?yún)?shù)最好是由malloc()函數(shù)返回的指針或指向全局變量的指針,而不要是指向局部變量的指針。要保證子線程處理參數(shù)時,該區(qū)域仍然有效。
--------------------------------------------------------------------------------
2.等待線程結(jié)束pthread_join
#include
int pthread_join(pthread_t tid, void **status);
返回值:函數(shù)成功返回0。任何其他返回值都表示錯誤。
等待一個線程結(jié)束。
該函數(shù)阻塞調(diào)用它線程,直到參數(shù)tid指定的線程結(jié)束。
tid指定的線程必須在當前進程中,同時tid指定的線程必須是非分離的。
不能有多個線程等待同一個線程終止。如果出現(xiàn)這種情況,一個線程將成功返回,別的線程將返回錯誤ESRCH。
如果參數(shù)status不為NULL,則將線程的退出狀態(tài)放在status指向的內(nèi)存中。
--------------------------------------------------------------------------------
3.分離線程pthread_detach
#include
int pthread_detach(pthread_t tid);
返回值:函數(shù)成功返回0。任何其他返回值都表示錯誤。
將非分離的線程設(shè)置為分離線程。即通知線程庫在指定的線程終止時回收線程占用的內(nèi)存等資源。
在一個線程上使用多次pthread_detach的結(jié)果是不可預見的。
--------------------------------------------------------------------------------
4.創(chuàng)建線程鍵pthread_key_create
#include
int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));
返回值:函數(shù)成功返回0。任何其他返回值都表示錯誤。
在進程中分配一個鍵值,這個鍵被用來表示一個線程數(shù)據(jù)項。這個鍵對進程中所有的線程都是可見的。剛創(chuàng)建線程數(shù)據(jù)鍵時,在所有線程中和這個鍵相關(guān)聯(lián)的值都是NULL。
函數(shù)成功返回后,分配的鍵放在key參數(shù)指向的內(nèi)存中,必須保證key參數(shù)指向的內(nèi)存區(qū)的有效性。
如果指定了解析函數(shù)destructor,那么當線程結(jié)束時并且將非空的值綁定在這個鍵上,系統(tǒng)將調(diào)用destructor函數(shù),參數(shù)就是相關(guān)線程與這個鍵綁定的值。綁定在這個鍵上的內(nèi)存塊可由destructor函數(shù)釋放。
--------------------------------------------------------------------------------
5.刪除線程鍵pthread_key_delete
#include
int pthread_key_delete(pthread_key_t key);
返回值:函數(shù)成功返回0。任何其他返回值都表示錯誤。
刪除線程數(shù)據(jù)鍵。這個鍵占用的內(nèi)存將被釋放,該鍵再被引用將返回錯誤。
在調(diào)用該函數(shù)之前,程序必須釋放和本線程相關(guān)聯(lián)的資源,該函數(shù)不會引發(fā)線程數(shù)據(jù)鍵的解析函數(shù)。
--------------------------------------------------------------------------------
6.設(shè)置線程數(shù)據(jù)pthread_setspecific
#include
int pthread_setspecific(pthread_key_t key, const void *value);
返回值:函數(shù)成功返回0。任何其他返回值都表示錯誤。
設(shè)置和某個線程數(shù)據(jù)鍵綁定在一起的線程專用數(shù)據(jù)(一般是指針)。
函數(shù)不會釋放原來綁定在鍵上的內(nèi)存,給一個鍵值綁定新的指針時,必須釋放原指針指向的內(nèi)存,否則會發(fā)生內(nèi)存泄漏。
--------------------------------------------------------------------------------
7.獲取線程數(shù)據(jù)pthread_getspecific
#include
void pthread_getspecific(pthread_key_t key, void **value);
無返回值。出錯時value指向NULL。
獲取綁定在線程數(shù)據(jù)鍵上的值,并在指定的位置存儲取來的值。
--------------------------------------------------------------------------------
8.獲取線程標示符pthread_self
#include
pthread_t pthread_self(void);
返回當前線程的標示符。
--------------------------------------------------------------------------------
9.比較線程pthread_equal
#include
int pthread_equal(pthread_t tid1, pthread_t tid2);
如果tid1和tid2相同,函數(shù)返回一個非0值,否則返回0。
如果tid1或tid2中任何一個是非法值,則返回將是不可預料的。
--------------------------------------------------------------------------------
10.一次執(zhí)行pthread_once
#include
int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));
返回值:函數(shù)成功返回0。任何其他返回值都表示錯誤。
函數(shù)用來調(diào)用初始化函數(shù)。如果已經(jīng)有線程通過pthread_once調(diào)用過這個初始化函數(shù)一次,那么以后通過pthread_once函數(shù)再調(diào)用這個初始化函數(shù)將無效。
參數(shù)once_control決定了相應的初始化函數(shù)是否被調(diào)用過。它一般如下使用:
[static] pthread_once_t once_control = PTHREAD_ONCE_INIT。
--------------------------------------------------------------------------------
11.出讓執(zhí)行權(quán)sched_yield
#include
int sched_yield(void);
返回值:函數(shù)成功返回0。-1表示錯誤。
把當前線程的執(zhí)行權(quán)(即對處理器的控制權(quán))出讓給另一個有相同或更高優(yōu)先級的線程。
--------------------------------------------------------------------------------
12.修改優(yōu)先級pthread_setschedparam
#include
int pthread_setschedparam(pthread_t tid, int policy, const struct sched_param *param);
返回值:函數(shù)成功返回0。任何其他返回值都表示錯誤。
修改線程的優(yōu)先權(quán)。
--------------------------------------------------------------------------------
13.獲取優(yōu)先級pthread_getschedparam
#include
int pthread_getschedparam(pthread_t tid, int policy, struct schedparam *param);
返回值:函數(shù)成功返回0。任何其他返回值都表示錯誤。
獲取線程的優(yōu)先級。
--------------------------------------------------------------------------------
14.發(fā)送信號pthread_kill
#include
int pthread_kill(pthread_t tid, int sig);
返回值:函數(shù)成功返回0。任何其他返回值都表示錯誤。
向tid指定的線程發(fā)送一個信號,tid指定的線程必須和當前線程在同一個進程中。
當sig參數(shù)為0時,函數(shù)將進行錯誤檢查,不發(fā)送信號,這常常用來檢查tid的合法性。
--------------------------------------------------------------------------------
15.設(shè)置線程掩碼pthread_sigmask
#include
#include
int pthread_sigmask(int how, const sigset_t *new, sigset_t *old);
返回值:函數(shù)成功返回0。任何其他返回值都表示錯誤。
改變或檢驗當前線程的信號掩碼。
參數(shù)how表示對當前信號掩碼進行什么操作,有如下值:SIG_BLOCK、SIG_UNBLOCK、SIG_SETMASK。
當參數(shù)new為NULL時,不論how的值是什么,當前線程的信號掩碼都不會改變。
舊的信號掩碼保存在參數(shù)old指向的內(nèi)存中,當old不為NULL時。
--------------------------------------------------------------------------------
16.終止線程pthread_exit
#include
void pthread_exit(void *status);
終止當前線程,所有綁定在線程數(shù)據(jù)鍵上的內(nèi)存將被釋放。如果當前線程是非分離的,那么這個線程的標示符合退出代碼將被保留,直到其他線程用pthread_join來等待當前線程的終止。如果當前線程是分離的,status將被忽略,線程標示符將被立即回收。
若status不為NULL,線程的退出代碼被置為status參數(shù)指向的值。
--------------------------------------------------------------------------------
17.退出線程pthread_cancel
#include
int pthread_cancel(pthread_t thread);
返回值:函數(shù)成功返回0。任何其他返回值都表示錯誤。
退出一個線程。如何響應退出請求取決于目標線程的狀態(tài)。
--------------------------------------------------------------------------------
18.允許/禁止退出線程pthread_setcancelstate
#include
int pthread_setcancelstate(int state, int *oldstate);
返回值:函數(shù)成功返回0。任何其他返回值都表示錯誤。
參數(shù)state取值為PTHREAD_CANCEL_ENABLE或PTHREAD_CANCEL_DISABLE。
--------------------------------------------------------------------------------
19.設(shè)置退出類型pthread_setcanceltype
#include
int pthread_setcanceltype(int type, int *oldtype);
返回值:函數(shù)成功返回0。任何其他返回值都表示錯誤。
將線程退出類型設(shè)置為延遲類型或異步類型。參數(shù)type的取值為PTHREAD_CANCEL_DEFERRED或PTHREAD_CANCEL_ASYNCHRONOUS。
當一個線程被創(chuàng)建后,缺省值是延遲類型。在異步方式下,線程可以在執(zhí)行的任何時候被退出。
--------------------------------------------------------------------------------
20.創(chuàng)建退出點pthread_testcancel
#include
void pthread_testcancel(void);
無返回值。
設(shè)置線程的退出點。
只有當線程的退出狀態(tài)是允許退出的,而且線程的退出類型是延遲時,調(diào)用該函數(shù)才有效。如果調(diào)用時線程的退出狀態(tài)是禁止的,則該調(diào)用不起作用。
小心使用該函數(shù),只有在能夠安全的被退出的地方才能夠設(shè)置退出點。
--------------------------------------------------------------------------------
21.壓入善后處理函數(shù)
#include
void pthread_cleanup_push(void (*routine)(void *), void *args);
將一個善后處理函數(shù)壓入善后處理函數(shù)堆棧。
--------------------------------------------------------------------------------
22.彈出善后處理函數(shù)
#include
void pthread_cleanup_pop(int execute);
從善后處理函數(shù)堆棧中彈出一個善后處理函數(shù)。如果參數(shù)execute非0,則執(zhí)行彈出的函數(shù);如果參數(shù)為0,則不執(zhí)行彈出函數(shù)。
如果一個線程顯式或隱式的調(diào)用pthread_exit()函數(shù)或線程接受了退出請求,線程庫實際上將會以非0參數(shù)調(diào)用pthread_cleanup_pop函數(shù)。