正确得到线程退出信息的方法详解-变量存储退出信息结构、使用动态存储的方式退出信息结构、使用全局变量方式推相互信息结构、使用main函数中的局部变量存储退出信息结构
#include <pthread.h>
void pthread_exit(void *value_ptr);
value_ptr:是线程的返回值。有pthread_join()检测获得。
功能:线程退出
#include<pthread.h>
int pthread_join(pthread_t thread, void**value_ptr);
功能:等待指定的线程结束。
返回值:成功返回0
参数:
thread:等待线程的ID(标示符)
value_ptr:用户自定义的指针,用来保存被等待线程的返回值。
下面程序使用局部变量存储退出信息结构、使用动态存储的方式退出信息结构、使用全局变量方式推相互信息结构、使用main函数中的局部变量存储退出信息结构,演示了不同的方式正确的到退出信息的方法。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct a
{
int b,c;
};//测试使用的结构体。
struct a r3;//全局变量
void *fn1(void *arg)//使用局部变量存储退出信息结构
{
struct a r1;
printf("first\n");
r1.b=1;
r1.c=2;
return (void *)&r1;
}
void *fn2(void *arg)//使用动态存储的方式退出信息结构
{
struct a *r2;
printf("second\n");
r2=(struct a *)malloc(sizeof(struct a));
r2->b=1;
r2->c=2;
return (void *)r2;
}
void *fn3(void *arg)//使用全局变量方式推相互信息结构
{
printf("third\n");
r3.b=1;
r3.c=2;
return (void *)(&r3);
}
void *fn4(void *arg)//使用main函数中的局部变量存储退出信息结构
{
struct a *r4=(struct a*)arg;
r4=(struct a *)malloc(sizeof(struct a));
printf("fourth\n");
r4->b=1;
r4->c=2;
return (void *)r4;
}
int main()
{
pthread_t tid1,tid2,tid3,tid4;
void *temp;
void *res;
int err;
err=pthread_create(&tid1,NULL,fn1,NULL);
if(err!=0)
{
printf("fail to fn1\n");
exit(1);
}
err=pthread_join(tid1,&res);
if(err!=0)
{
printf("can't join\n");
exit(1);
}
printf("1st result :%d,%d\n",((struct a*)res)->b,((struct a*)res)->c);
err=pthread_create(&tid2,NULL,fn2,NULL);
if(err!=0)
{
printf("fail to fn2\n");
exit(1);
}
err=pthread_join(tid2,&res);
if(err!=0)
{
printf("can't join\n");
exit(1);
}
printf("2st result :%d,%d\n",((struct a*)res)->b,((struct a*)res)->c);
err=pthread_create(&tid3,NULL,fn3,NULL);
if(err!=0)
{
printf("fail to fn3\n");
exit(1);
}
err=pthread_join(tid3,&res);
if(err!=0)
{
printf("can't join\n");
exit(1);
}
printf("3st result :%d,%d\n",((struct a*)res)->b,((struct a*)res)->c);
err=pthread_create(&tid4,NULL,fn4,temp);
if(err!=0)
{
printf("fail to fn1\n");
exit(1);
}
err=pthread_join(tid4,&res);
if(err!=0)
{
printf("can't join\n");
exit(1);
}
printf("4st result :%d,%d\n",((struct a*)res)->b,((struct a*)res)->c);
return 0;
#include <pthread.h>
void pthread_exit(void *value_ptr);
value_ptr:是线程的返回值。有pthread_join()检测获得。
功能:线程退出
#include<pthread.h>
int pthread_join(pthread_t thread, void**value_ptr);
功能:等待指定的线程结束。
返回值:成功返回0
参数:
thread:等待线程的ID(标示符)
value_ptr:用户自定义的指针,用来保存被等待线程的返回值。
分析如何正确得到:在线程结束运行后,Linux内核中保存只是内存退出内存区域的首地址,而并非将退出信息实际保存在内核中,因此,在线程结束运行后,其保存退出信息的内存区域仍然是有效的,所以不能把退出信息存储在局部变量中,而应使用动态分配的内存或是全局变量。
下面程序使用局部变量存储退出信息结构、使用动态存储的方式退出信息结构、使用全局变量方式推相互信息结构、使用main函数中的局部变量存储退出信息结构,演示了不同的方式正确的到退出信息的方法。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct a
{
int b,c;
};//测试使用的结构体。
struct a r3;//全局变量
void *fn1(void *arg)//使用局部变量存储退出信息结构
{
struct a r1;
printf("first\n");
r1.b=1;
r1.c=2;
return (void *)&r1;
}
void *fn2(void *arg)//使用动态存储的方式退出信息结构
{
struct a *r2;
printf("second\n");
r2=(struct a *)malloc(sizeof(struct a));
r2->b=1;
r2->c=2;
return (void *)r2;
}
void *fn3(void *arg)//使用全局变量方式推相互信息结构
{
printf("third\n");
r3.b=1;
r3.c=2;
return (void *)(&r3);
}
void *fn4(void *arg)//使用main函数中的局部变量存储退出信息结构
{
struct a *r4=(struct a*)arg;
r4=(struct a *)malloc(sizeof(struct a));
printf("fourth\n");
r4->b=1;
r4->c=2;
return (void *)r4;
}
int main()
{
pthread_t tid1,tid2,tid3,tid4;
void *temp;
void *res;
int err;
err=pthread_create(&tid1,NULL,fn1,NULL);
if(err!=0)
{
printf("fail to fn1\n");
exit(1);
}
err=pthread_join(tid1,&res);
if(err!=0)
{
printf("can't join\n");
exit(1);
}
printf("1st result :%d,%d\n",((struct a*)res)->b,((struct a*)res)->c);
err=pthread_create(&tid2,NULL,fn2,NULL);
if(err!=0)
{
printf("fail to fn2\n");
exit(1);
}
err=pthread_join(tid2,&res);
if(err!=0)
{
printf("can't join\n");
exit(1);
}
printf("2st result :%d,%d\n",((struct a*)res)->b,((struct a*)res)->c);
err=pthread_create(&tid3,NULL,fn3,NULL);
if(err!=0)
{
printf("fail to fn3\n");
exit(1);
}
err=pthread_join(tid3,&res);
if(err!=0)
{
printf("can't join\n");
exit(1);
}
printf("3st result :%d,%d\n",((struct a*)res)->b,((struct a*)res)->c);
err=pthread_create(&tid4,NULL,fn4,temp);
if(err!=0)
{
printf("fail to fn1\n");
exit(1);
}
err=pthread_join(tid4,&res);
if(err!=0)
{
printf("can't join\n");
exit(1);
}
printf("4st result :%d,%d\n",((struct a*)res)->b,((struct a*)res)->c);
return 0;
}
运行结果