码迷,mamicode.com
首页 > 编程语言 > 详细

线程回收

时间:2019-08-23 19:29:16      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:函数返回值   waitpid   col   nis   led   printf   -668   调用   函数   

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\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

 

线程回收

标签:函数返回值   waitpid   col   nis   led   printf   -668   调用   函数   

原文地址:https://www.cnblogs.com/wanghao-boke/p/11389714.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!