日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pthread_once()

發(fā)布時(shí)間:2025/3/15 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pthread_once() 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在asterisk 開源Sip系統(tǒng)中,經(jīng)常會(huì)看到這個(gè)函數(shù)的使用,一直不理解,特意有了下面的總結(jié),但是到現(xiàn)在也沒用理解這個(gè)函數(shù)在Sip協(xié)議中使用的方法!

在多線程環(huán)境中,有些事僅需要執(zhí)行一次。通常當(dāng)初始化應(yīng)用程序時(shí),可以比較容易地將其放在main函數(shù)中。但當(dāng)你寫一個(gè)庫時(shí),就不能在main里面初始化了,你可以用靜態(tài)初始化,但使用一次初始化(pthread_once)會(huì)比較容易些。


int pthread_once(pthread_once_t *once_control, void (*init_routine) (void));

功能:本函數(shù)使用初值為PTHREAD_ONCE_INIT的once_control變量保證init_routine()函數(shù)在本進(jìn)程執(zhí)行序列中僅執(zhí)行一次。

?
在多線程編程環(huán)境下,盡管pthread_once()調(diào)用會(huì)出現(xiàn)在多個(gè)線程中,init_routine()函數(shù)僅執(zhí)行一次,究竟在哪個(gè)線程中執(zhí)行是不定的,是由內(nèi)核調(diào)度來決定。

Linux Threads使用互斥鎖和條件變量保證由pthread_once()指定的函數(shù)執(zhí)行且僅執(zhí)行一次,而once_control表示是否執(zhí)行過。

如果once_control的初值不是PTHREAD_ONCE_INIT(Linux Threads定義為0),pthread_once() 的行為就會(huì)不正常。

在LinuxThreads中,實(shí)際"一次性函數(shù)"的執(zhí)行狀態(tài)有三種:NEVER(0)、IN_PROGRESS(1)、DONE (2),如果once初值設(shè)為1,則由于所有pthread_once()都必須等待其中一個(gè)激發(fā)"已執(zhí)行一次"信號(hào),因此所有pthread_once ()都會(huì)陷入永久的等待中;如果設(shè)為2,則表示該函數(shù)已執(zhí)行過一次,從而所有pthread_once()都會(huì)立即返回0

具體的一個(gè)實(shí)例:

[csharp] view plaincopy print?
  • #include<iostream>??
  • #include<pthread.h>??
  • using?namespace?std;??
  • ??
  • pthread_once_t?once?=?PTHREAD_ONCE_INIT;??
  • ??
  • void?once_run(void)??
  • {??
  • ????????cout<<"once_run?in?thread?"<<(unsigned?int?)pthread_self()<<endl;??
  • }??
  • ??
  • void?*?child1(void?*?arg)??
  • {??
  • ????????pthread_t?tid?=pthread_self();??
  • ????????cout<<"thread?"<<(unsigned?int?)tid<<"?enter"<<endl;??
  • ????????pthread_once(&once,once_run);??
  • ????????cout<<"thread?"<<tid<<"?return"<<endl;??
  • }??
  • ??
  • ??
  • void?*?child2(void?*?arg)??
  • {??
  • ????????pthread_t?tid?=pthread_self();??
  • ????????cout<<"thread?"<<(unsigned?int?)tid<<"?enter"<<endl;??
  • ????????pthread_once(&once,once_run);??
  • ????????cout<<"thread?"<<tid<<"?return"<<endl;??
  • }??
  • ??
  • int?main(void)??
  • {??
  • ????????pthread_t?tid1,tid2;??
  • ????????cout<<"hello"<<endl;??
  • ????????pthread_create(&tid1,NULL,child1,NULL);??
  • ????????pthread_create(&tid2,NULL,child2,NULL);??
  • ????????sleep(10);??
  • ????????cout<<"main?thread?exit"<<endl;??
  • ????????return?0;??
  • ??
  • }??
  • #include<iostream> #include<pthread.h> using namespace std;pthread_once_t once = PTHREAD_ONCE_INIT;void once_run(void) {cout<<"once_run in thread "<<(unsigned int )pthread_self()<<endl; }void * child1(void * arg) {pthread_t tid =pthread_self();cout<<"thread "<<(unsigned int )tid<<" enter"<<endl;pthread_once(&once,once_run);cout<<"thread "<<tid<<" return"<<endl; }void * child2(void * arg) {pthread_t tid =pthread_self();cout<<"thread "<<(unsigned int )tid<<" enter"<<endl;pthread_once(&once,once_run);cout<<"thread "<<tid<<" return"<<endl; }int main(void) {pthread_t tid1,tid2;cout<<"hello"<<endl;pthread_create(&tid1,NULL,child1,NULL);pthread_create(&tid2,NULL,child2,NULL);sleep(10);cout<<"main thread exit"<<endl;return 0;}

    執(zhí)行結(jié)果:

    [csharp] view plaincopy print?
  • hello??
  • thread?3086535584?enter??
  • once_run?in?thread?3086535584??
  • thread?3086535584?return??
  • thread?3076045728?enter??
  • thread?3076045728?return??
  • main?thread?exit??
  • 總結(jié)

    以上是生活随笔為你收集整理的pthread_once()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。