国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Posix線程編程指南(5)
userphoto

2011.01.10

關注

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ù)的定義和使用。

獲得本線程ID

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是否相等。





回頁首


僅執(zhí)行一次的操作

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()是一樣的,其中原因目前還不清楚。

本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Posix線程編程指南
pthread編程基礎
linux 線程 進程經(jīng)典文章
Linux下編程為什么多用進程少用線程-編程討論之二--Linux軟件下載源碼編程文章資料...
linux 系統(tǒng)文件操作sem_open, sem_close, sem_post, sem_wait
線程同步、條件變量、互斥鎖的使用
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服