pthread_join函数
int pthread_join(pthread_t thread, void **retval);
作用:阻塞等待線程退出,獲取線程退出狀態(tài)。其作用對(duì)應(yīng)進(jìn)程中 waitpid() 函數(shù)。
成功:0;失敗:錯(cuò)誤號(hào) ??strerror函數(shù)
參數(shù):thread:線程ID (注意:不是指針);retval:存儲(chǔ)線程結(jié)束狀態(tài)。
對(duì)比記憶:在wait回收子進(jìn)程時(shí),子進(jìn)程exit或return返回的值即為status的值(int類型),wait函數(shù)第一個(gè)形參為回收進(jìn)程的pid,第二個(gè)形參為&status(int *類型指針);對(duì)于回收子線程時(shí),子線程退出的值(pthread_exit函數(shù),或return返回的值)都為void *類型,則pthread_join函數(shù)的第一個(gè)形參為線程ID(pthread_t類型),第二個(gè)形參為void **類型。
調(diào)用該函數(shù)的線程將掛起等待,直到id為thread的線程終止。thread線程以不同的方法終止,通過pthread_join得到的終止?fàn)顟B(tài)是不同的,總結(jié)如下:
對(duì)應(yīng)于僵尸進(jìn)程,也是存在僵尸線程。因此,在線程結(jié)束時(shí),也需要對(duì)線程進(jìn)行回收以釋放僵尸線程所占用的資源。對(duì)進(jìn)程的回收有區(qū)別的就是,一個(gè)進(jìn)程中的所有線程都是可以互相回收的,即不是只能由主控線程回收子線程,兄弟線程之間也可以回收。一次pthread_join只能回收一個(gè)線程。回收線程時(shí)也可以不關(guān)心線程結(jié)束時(shí)的狀態(tài),pthread_join函數(shù)的參數(shù)傳NULL即可,此時(shí)不關(guān)心線程結(jié)束時(shí)的狀態(tài),只是將其回收并釋放線程所占用的資源。
//循環(huán)創(chuàng)建n個(gè)子線程,并回收這n個(gè)子線程
#include <stdio.h> #include <pthread.h> #include <string.h> #include <unistd.h> #include <stdlib.h>typedef struct {int a;char b;char str[10]; }exit_t; //返回值為一個(gè)結(jié)構(gòu)體void *ftn( void *arg ) {int s;exit_t *retval; //retval的地址在棧空間,但是retval本身卻位與堆空間(heap),malloc所分配的。retval=(exit_t *)malloc( sizeof(exit_t) ); //必須用malloc分配,不能用線程??臻gs=(int)arg; //注意只能傳值,不能傳地址(用于判別是哪個(gè)線程)if( s<=1 ){retval->a=10;retval->b='a';strcpy(retval->str,"zsx");}if( s>1 && s<=3 ){retval->a=20;retval->b='b';strcpy(retval->str,"rgf");}if( s>3 ){retval->a=30;retval->b='c';strcpy(retval->str,"zy");}printf("I am %dth thread, and my ID is %lu.\n",s+1,pthread_self( ));pthread_exit((void *)retval); //或者 return (void *)retval; 兩者等價(jià)! }int main(int argc, char *argv[ ]) {int n=5, i, ret;if( argc==2 )n=atoi(argv[1]);pthread_t tid[n];for(i=0;i<n;i++){ret=pthread_create(&tid[i],NULL,ftn,(void *)i); //注意只能傳值,不能傳地址,因?yàn)榈刂穼?duì)應(yīng)的i值會(huì)變化if( ret!=0){fprintf(stderr,"pthread_create error: %s\n",strerror(ret));exit(1);}}for(i=0;i<n;i++) //回收每個(gè)子線程{exit_t *re; //re位于主控線程的??臻g,但是re本身的值為子線程傳給它的值。ret=pthread_join( tid[i],(void **)&re);if( ret!=0){fprintf(stderr,"pthread_join error: %s\n",strerror(ret));exit(1);}printf("the %dth thread: a=%d, b=%c, str=%s.\n",i+1,re->a,re->b,re->str);free(re); //注意必須釋放malloc分配的空間,防止內(nèi)存泄漏re = NULL; //置空,防止使用幽靈指針}printf("In main: the PID is %d, and the TID is %lu.\n",getpid( ),pthread_self( ));pthread_exit((void *)1); }[root@localhost 01_pthread_test]# ./ptrd_join
I am 4th thread, and my ID is 4124101440.
I am 2th thread, and my ID is 4140886848.
I am 1th thread, and my ID is 4149279552.
I am 5th thread, and my ID is 4115708736.
I am 3th thread, and my ID is 4132494144.
the 1th thread: a=10, b=a, str=zsx.
the 2th thread: a=10, b=a, str=zsx.
the 3th thread: a=20, b=b, str=rgf.
the 4th thread: a=20, b=b, str=rgf.
the 5th thread: a=30, b=c, str=zy.
In main: the PID is 10415, and the TID is 4151490816.
分析討論:
總結(jié)
以上是生活随笔為你收集整理的pthread_join函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 线程共享全局变量(.data和.bbs)
- 下一篇: 成都欢乐谷只蹦极也需要买门票么