线程回收
pthread_join函數(shù)
阻塞等待線程退出,獲取線程退出狀態(tài)????????? 其作用,對應(yīng)進程中 waitpid() 函數(shù)。
?????? int pthread_join(pthread_t thread, void **retval); 成功:0;失敗:錯誤號
?????? 參數(shù):thread:線程ID (【注意】:不是指針);retval:存儲線程結(jié)束狀態(tài)。
?????? 對比記憶:
????????????? 進程中:main返回值、exit參數(shù)-->int;等待子進程結(jié)束 wait 函數(shù)參數(shù)-->int *
????????????? 線程中:線程主函數(shù)返回值、pthread_exit-->void *;等待線程結(jié)束 pthread_join 函數(shù)參數(shù)-->void **
【練習】:參數(shù) retval 非空用法。??????????????????????????????????????????????????????????????????????????????? ?????? 【pthrd_exit_join.c】
調(diào)用該函數(shù)的線程將掛起等待,直到id為thread的線程終止。thread線程以不同的方法終止,通過pthread_join得到的終止狀態(tài)是不同的,總結(jié)如下:
?
【練習】:使用pthread_join函數(shù)將循環(huán)創(chuàng)建的多個子線程回收。? ? ? ? ? ??
/*** 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); }運行結(jié)果:
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); }運行結(jié)果:
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; }運行結(jié)果:
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
?
轉(zhuǎn)載于:https://www.cnblogs.com/wanghao-boke/p/11389714.html
總結(jié)
- 上一篇: 英雄联盟手游为什么不发标
- 下一篇: 多进程服务器