• 线程回收


    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得到的终止状态是不同的,总结如下:

    1. 如果thread线程通过return返回,retval所指向的单元里存放的是thread线程函数的返回值。
    2. 如果thread线程被别的线程调用pthread_cancel异常终止掉,retval所指向的单元里存放的是常数PTHREAD_CANCELED。
    3. 如果thread线程是自己调用pthread_exit终止的,retval所指向的单元存放的是传给pthread_exit的参数。
    4. 如果对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
    ",pthread_self(),getpid());
    
        ret = pthread_create(&tid,NULL,thrd_func,NULL);
        if(0 != ret)
        {
            fprintf(stderr,"pthread_create error : %s 
    ",strerror(ret));
            exit(1);
        }
    
        pthread_join(tid,(void **)&retval);
        printf("--------------%d
    ",(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
    ");
    
        pthread_exit((exit_t *)retvar);
    }
    
    int main()
    {
        pthread_t tid;
        int ret;
        exit_t *retval;
    
        printf("In main 1 : thread id = %lu, pid = %u
    ",pthread_self(),getpid());
    
        ret = pthread_create(&tid,NULL,thrd_func,NULL);
        if(0 != ret)
        {
            fprintf(stderr,"pthread_create error : %s 
    ",strerror(ret));
            exit(1);
        }
    
        pthread_join(tid,(void **)&retval);
        printf("ch = %c,var = %d,str = %s
    --------------%d
    ",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
    ",var);
            return (void*)var;
        }
        else if( 3 == i)
        {
            var = 888;
            printf("i'm %dth pthread,pthread id = %lu ,var = %d
    ",i+1,pthread_self(),var);
            pthread_exit((void*)var);
        }
        else
        {
            printf("i'm %dth pthread,pthread id = %lu ,var = %d
    ",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
    ",(int)ret[i]);
        }
    
        printf("I'm main pthread tid = %lu var = %d
    ",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

  • 相关阅读:
    Windows 10下CUDA及cuDNN的安装 —— Pytorch
    Centos7 python3环境搭建 兼容python2.7
    VMware中Linux虚拟机与Windows主机共享文件夹
    基于阿里云服务器的网站搭建 新手教程
    CVE-2017-11882 漏洞分析总结 新手漏洞分析详细教程
    Linux基本命令 和 Regex 正则表达式
    Shell 编程 基础用法
    Perl 编程 基础用法
    Python3 网络通信 网络聊天室 文件传输
    Ant Design of Angular
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11389714.html
Copyright © 2020-2023  润新知