2001 年 11 月 01 日
這是一個關于Posix線程編程的專欄。作者在闡明概念的基礎上,將向您詳細講述Posix線程庫API。本文是第五篇將向您講述pthread_self()、pthread_equal()和pthread_once()等雜項函數(shù)。
在Posix線程規(guī)范中還有幾個輔助函數(shù)難以歸類,暫且稱其為雜項函數(shù),主要包括pthread_self()、pthread_equal()和pthread_once()三個,另外還有一個LinuxThreads非可移植性擴展函數(shù)pthread_kill_other_threads_np()。本文就介紹這幾個函數(shù)的定義和使用。
pthread_t pthread_self(void)
本函數(shù)返回本線程的標識符。
在LinuxThreads中,每個線程都用一個pthread_descr結構來描述,其中包含了線程狀態(tài)、線程ID等所有需要的數(shù)據(jù)結構,此函數(shù)的實現(xiàn)就是在線程棧幀中找到本線程的pthread_descr結構,然后返回其中的p_tid項。
pthread_t類型在LinuxThreads中定義為無符號長整型。
![]() ![]() |
![]()
|
int pthread_equal(pthread_t thread1, pthread_t thread2)
判斷兩個線程描述符是否指向同一線程。在LinuxThreads中,線程ID相同的線程必然是同一個線程,因此,這個函數(shù)的實現(xiàn)僅僅判斷thread1和thread2是否相等。
![]() ![]() |
![]()
|
int pthread_once(pthread_once_t *once_control, void (*init_routine) (void))
本函數(shù)使用初值為PTHREAD_ONCE_INIT的once_control變量保證init_routine()函數(shù)在本進程執(zhí)行序列中僅執(zhí)行一次。
#include <stdio.h> #include <pthread.h> pthread_once_t once=PTHREAD_ONCE_INIT; void once_run(void) { printf("once_run in thread %d\n",pthread_self()); } void * child1(void *arg) { int tid=pthread_self(); printf("thread %d enter\n",tid); pthread_once(&once,once_run); printf("thread %d returns\n",tid); } void * child2(void *arg) { int tid=pthread_self(); printf("thread %d enter\n",tid); pthread_once(&once,once_run); printf("thread %d returns\n",tid); } int main(void) { int tid1,tid2; printf("hello\n"); pthread_create(&tid1,NULL,child1,NULL); pthread_create(&tid2,NULL,child2,NULL); sleep(10); printf("main thread exit\n"); return 0; } |
once_run()函數(shù)僅執(zhí)行一次,且究竟在哪個線程中執(zhí)行是不定的,盡管pthread_once(&once,once_run)出現(xiàn)在兩個線程中。
LinuxThreads使用互斥鎖和條件變量保證由pthread_once()指定的函數(shù)執(zhí)行且僅執(zhí)行一次,而once_control則表征是否執(zhí)行過。如果once_control的初值不是PTHREAD_ONCE_INIT(LinuxThreads定義為0),pthread_once()的行為就會不正常。在LinuxThreads中,實際"一次性函數(shù)"的執(zhí)行狀態(tài)有三種:NEVER(0)、IN_PROGRESS(1)、DONE(2),如果once初值設為1,則由于所有pthread_once()都必須等待其中一個激發(fā)"已執(zhí)行一次"信號,因此所有pthread_once()都會陷入永久的等待中;如果設為2,則表示該函數(shù)已執(zhí)行過一次,從而所有pthread_once()都會立即返回0。
![]() ![]() |
![]()
|
pthread_kill_other_threads_np()
void pthread_kill_other_threads_np(void)
這個函數(shù)是LinuxThreads針對本身無法實現(xiàn)的POSIX約定而做的擴展。POSIX要求當進程的某一個線程執(zhí)行exec*系統(tǒng)調(diào)用在進程空間中加載另一個程序時,當前進程的所有線程都應終止。由于LinuxThreads的局限性,該機制無法在exec中實現(xiàn),因此要求線程執(zhí)行exec前手工終止其他所有線程。pthread_kill_other_threads_np()的作用就是這個。
需要注意的是,pthread_kill_other_threads_np()并沒有通過pthread_cancel()來終止線程,而是直接向管理線程發(fā)"進程退出"信號,使所有其他線程都結束運行,而不經(jīng)過Cancel動作,當然也不會執(zhí)行退出回調(diào)函數(shù)。盡管LinuxThreads的實驗結果與文檔說明相同,但代碼實現(xiàn)中卻是用的__pthread_sig_cancel信號來kill線程,應該效果與執(zhí)行pthread_cancel()是一樣的,其中原因目前還不清楚。