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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

线程回收

發(fā)布時間:2023/11/30 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 线程回收 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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é)如下:

  • 如果thread線程通過return返回,retval所指向的單元里存放的是thread線程函數(shù)的返回值。
  • 如果thread線程被別的線程調(diào)用pthread_cancel異常終止掉,retval所指向的單元里存放的是常數(shù)PTHREAD_CANCELED。
  • 如果thread線程是自己調(diào)用pthread_exit終止的,retval所指向的單元存放的是傳給pthread_exit的參數(shù)。
  • 如果對thread線程的終止狀態(tài)不感興趣,可以傳NULL給retval參數(shù)。
  • ?

    【練習】:使用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é)

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

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