pthread_join函數
阻塞等待線程退出,獲取線程退出狀態????????? 其作用,對應進程中 waitpid() 函數。
?????? int pthread_join(pthread_t thread, void **retval); 成功:0;失敗:錯誤號
?????? 參數:thread:線程ID (【注意】:不是指針);retval:存儲線程結束狀態。
?????? 對比記憶:
????????????? 進程中:main返回值、exit參數-->int;等待子進程結束 wait 函數參數-->int *
????????????? 線程中:線程主函數返回值、pthread_exit-->void *;等待線程結束 pthread_join 函數參數-->void **
【練習】:參數 retval 非空用法。??????????????????????????????????????????????????????????????????????????????? ?????? 【pthrd_exit_join.c】
調用該函數的線程將掛起等待,直到id為thread的線程終止。thread線程以不同的方法終止,通過pthread_join得到的終止狀態是不同的,總結如下:
- 如果thread線程通過return返回,retval所指向的單元里存放的是thread線程函數的返回值。
- 如果thread線程被別的線程調用pthread_cancel異常終止掉,retval所指向的單元里存放的是常數PTHREAD_CANCELED。
- 如果thread線程是自己調用pthread_exit終止的,retval所指向的單元存放的是傳給pthread_exit的參數。
- 如果對thread線程的終止狀態不感興趣,可以傳NULL給retval參數。
?
【練習】:使用pthread_join函數將循環創建的多個子線程回收。? ? ? ? ? ??
/*** thread_join.c ***/ #include<stdio.h> #include<error.h> #include<string.h> #include<unistd.h> #include<stdlib.h> #include<pthread.h>typedef struct {char ch;int var;char str[64]; }exit_t;void *thrd_func(void *arg) {pthread_exit((void *)1); }int main() {pthread_t tid;int ret;int *retval;printf("In main 1 : thread id = %lu, pid = %u\n",pthread_self(),getpid());ret = pthread_create(&tid,NULL,thrd_func,NULL);if(0 != ret){fprintf(stderr,"pthread_create error : %s \n",strerror(ret));exit(1);}pthread_join(tid,(void **)&retval);printf("--------------%d\n",(int)retval);pthread_exit((void*)1); }
運行結果:
ubuntu1604@ubuntu:~/wangqinghe/linux/20190819$ ./thread_join
In main 1 : thread id = 139721544345344, pid = 12974
--------------1
/*** pthread_join_struct.c ***/ #include<stdio.h> #include<error.h> #include<string.h> #include<unistd.h> #include<stdlib.h> #include<pthread.h>typedef struct {char ch;int var;char str[64]; }exit_t;void *thrd_func(void *arg) {exit_t *retvar = (exit_t *)malloc(sizeof(exit_t));retvar->ch = 'm';retvar->var = 200;strcpy(retvar->str,"my thread\n");pthread_exit((exit_t *)retvar); }int main() {pthread_t tid;int ret;exit_t *retval;printf("In main 1 : thread id = %lu, pid = %u\n",pthread_self(),getpid());ret = pthread_create(&tid,NULL,thrd_func,NULL);if(0 != ret){fprintf(stderr,"pthread_create error : %s \n",strerror(ret));exit(1);}pthread_join(tid,(void **)&retval);printf("ch = %c,var = %d,str = %s\n--------------%d\n",retval->ch,retval->var,retval->str);free(retval);pthread_exit((void*)1); }
運行結果:
ubuntu1604@ubuntu:~/wangqinghe/linux/20190819$ ./thread_join
In main 1 : thread id = 140316487100160, pid = 13121
ch = m,var = 200,str = my thread
?
--------------202
/*** thread_join_loop.c ***/ #include<stdio.h> #include<string.h> #include<unistd.h> #include<pthread.h>int var = 100;void *tfn(void *arg) {int i;i = (int)arg;sleep(i);if(1 == i){var = 333;printf("var = %d\n",var);return (void*)var;}else if( 3 == i){var = 888;printf("i'm %dth pthread,pthread id = %lu ,var = %d\n",i+1,pthread_self(),var);pthread_exit((void*)var);}else{printf("i'm %dth pthread,pthread id = %lu ,var = %d\n",i+1,pthread_self(),var);pthread_exit((void*)var); }return NULL; }int main() {pthread_t tid[5];int i;int *ret[5];for(i = 0; i < 5; i++){pthread_create(&tid[i],NULL,tfn,(void*)i);}for(i = 0; i < 5; i++){pthread_join(tid[i],(void**)&ret[i]);printf("-----------%d's ret = %d\n",(int)ret[i]);}printf("I'm main pthread tid = %lu var = %d\n",pthread_self(),var);sleep(i);return 0; }
運行結果:
ubuntu1604@ubuntu:~/wangqinghe/linux/20190819$ ./thread_loop
i'm 1th pthread,pthread id = 139809124009728 ,var = 100
-----------100's ret = -645238160
var = 333
-----------333's ret = -651408960
i'm 3th pthread,pthread id = 139809107224320 ,var = 333
-----------333's ret = -659801664
i'm 4th pthread,pthread id = 139809098831616 ,var = 888
-----------888's ret = -668194368
i'm 5th pthread,pthread id = 139809090438912 ,var = 888
-----------888's ret = 0
I'm main pthread tid = 139809132349184 var = 888
?