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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux中pthread_join()与pthread_detach()详解

發(fā)布時間:2025/6/15 linux 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux中pthread_join()与pthread_detach()详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言:

1.linux線程執(zhí)行和windows不同,pthread有兩種狀態(tài)joinable狀態(tài)和unjoinable狀態(tài),如果線程是joinable狀態(tài),當(dāng)線程函數(shù)自己返回退出時或pthread_exit時都不會釋放線程所占用堆棧和線程描述符(總計8K多)。只有當(dāng)你調(diào)用了pthread_join之后這些資源才會被釋放。若是unjoinable狀態(tài)的線程,這些資源在線程函數(shù)退出時或pthread_exit時自動會被釋放。

2.unjoinable屬性可以在pthread_create時指定,或在線程創(chuàng)建后在線程中pthread_detach自己, 如:pthread_detach(pthread_self()),將狀態(tài)改為unjoinable狀態(tài),確保資源的釋放。或者將線程置為 joinable,然后適時調(diào)用pthread_join.

3.其實簡單的說就是在線程函數(shù)頭加上 pthread_detach(pthread_self())的話,線程狀態(tài)改變,在函數(shù)尾部直接 pthread_exit線程就會自動退出。省去了給線程擦屁股的麻煩。

eg:

?pthread_t tid;int status = pthread_create(&tid, NULL, ThreadFunc, NULL);if(status != 0){perror("pthread_create error");}pthread_detach(tid);

一:pthread_join()

(1)pthread_join()即是子線程合入主線程,主線程阻塞等待子線程結(jié)束,然后回收子線程資源。

(2)函數(shù)說明

1)頭文件 : #include <pthread.h>

2)函數(shù)定義: int pthread_join(pthread_t thread, void **retval);

3)描述 :pthread_join()函數(shù),以阻塞的方式等待thread指定的線程結(jié)束。當(dāng)函數(shù)返回時,被等待線程的資源被收回。如果線程已經(jīng)結(jié)束,那么該函數(shù)會立即返回。并且thread指定的線程必須是joinable的。

4)參數(shù) :thread: 線程標(biāo)識符,即線程ID,標(biāo)識唯一線程。retval: 用戶定義的指針,用來存儲被等待線程的返回值。

5)返回值 : 0代表成功。 失敗,返回的則是錯誤號。

(3)實例

#include <pthread.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h>void *thread_function(void *arg) {int i;for ( i=0; i<8; i++){printf("Thread working...! %d \n",i);sleep(1);}return NULL; }int main(void) {pthread_t mythread;if ( pthread_create( &mythread, NULL, thread_function, NULL) ){printf("error creating thread.");abort();}if ( pthread_join ( mythread, NULL ) ){printf("error join thread.");abort();}printf("thread done! \n");exit(0); }

結(jié)果:

去掉pthread_join ( mythread, NULL )

#include <stdlib.h> #include <unistd.h> #include <stdio.h>void *thread_function(void *arg) {int i;for ( i=0; i<8; i++){printf("Thread working...! %d \n",i);sleep(1);}return NULL; }int main(void) {pthread_t mythread;if ( pthread_create( &mythread, NULL, thread_function, NULL) ){printf("error creating thread.");abort();} /*if ( pthread_join ( mythread, NULL ) ){printf("error join thread.");abort();} */printf("thread done! \n");exit(0); }

結(jié)果:

二:pthread_detach()

(1)pthread_detach()即主線程與子線程分離,子線程結(jié)束后,資源自動回收。

(2)函數(shù)說明

1)函數(shù)原型:int pthread_detach(pthread_t tid);

2)功能:pthread_join()函數(shù)的替代函數(shù),可回收創(chuàng)建時detachstate屬性設(shè)置為PTHREAD_CREATE_JOINABLE的線程的存儲空間。該函數(shù)不會阻塞父線程。pthread_join()函數(shù)用于只是應(yīng)用程序在線程tid終止時回收其存儲空間。如果tid尚未終止,pthread_detach()不會終止該線程。當(dāng)然pthread_detach(pthread_self())也是可以得

3)頭文件:#include <pthread.h> ? ? pthread非linux系統(tǒng)的默認(rèn)庫, 需手動鏈接-線程庫 -lpthread

4)參數(shù):tid:線程標(biāo)識符

5)返回值:pthread_detach() 在調(diào)用成功完成之后返回零。其他任何返回值都表示出現(xiàn)了錯誤。如果檢測到以下任一情況,pthread_detach()將失敗并返回相應(yīng)的值。

EINVAL:tid是分離線程

ESRCH:tid不是當(dāng)前進程中有效的為分離線程

(3)實例

#include <stdlib.h> #include <unistd.h> #include <stdio.h> void print_message_function( void *ptr ); main ( ) {pthread_t thread1;pthread_create(&thread1,NULL,(void *)&print_message_function,(void *)0);int i;for(i=0;i<5;i++){printf("%d\n",thread1);}exit (0) ;} void print_message_function( void *ptr ) { pthread_detach(pthread_self());static int g;printf("%d\n", g++);pthread_exit(0) ;}

結(jié)果:

//pthread_detach(pthread_self());
//使線成分離出來。當(dāng)這個線程執(zhí)行完成任務(wù)后釋放釋放資源。不然它會保留退出狀態(tài),等待別人來取。
pthread_detach(threadid)和pthread_detach(pthread_self())沒有什么區(qū)別吧!有很嚴(yán)格的區(qū)別嗎???如果非要講區(qū)別不可,我覺得應(yīng)該是調(diào)用他們的線程不同。
? ? ? pthread_detach(threadid)函數(shù)的功能是使線程ID為threadid的線程處于分離狀態(tài),一旦線程處于分離狀態(tài),該線程終止時底 層資源立即被回收;否則終止子線程的狀態(tài)會一直保存(占用系統(tǒng)資源)直到主線程調(diào)用pthread_join(threadid,NULL)獲取線程的退 出狀態(tài)。通常是主線程使用pthread_create()創(chuàng)建子線程以后,一般可以調(diào)用pthread_detach(threadid)分離剛剛創(chuàng)建的子線程,這里的threadid是指子線程的threadid;如此以來,該子線程止時底層資源立即被回收;被創(chuàng)建的子線程也可以自己分離自己,子線程調(diào)用pthread_detach(pthread_self())就是分離自己,因為pthread_self()這個函數(shù)返回的就是自己本身的線程ID;

總結(jié)

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

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