需要注意:所使用的存储返回信息的结构,在调用者完成调用以后,必须仍然有效。
实例程序:
#include "apue.h"
#include <pthread.h>
void * thread1_fun(void *arg)
{
printf("Thread 1 is runing\n");
return ((void *) 1);
}
void * thread2_fun(void *arg)
{
printf("Thread 2 is runing\n");
pthread_exit((void *) 2);
}
int main()
{
pthread_t td1,td2;
int err;
void *ret_ptr;
err = pthread_create(&td1,NULL,thread1_fun,NULL);
if (err != 0){
err_sys("thread1 create failed");
}
err = pthread_create(&td2,NULL,thread2_fun,NULL);
if (err != 0)
err_sys("Thread2 create failed");
err = pthread_join(td1,&ret_ptr);
if (err != 0)
err_exit(err,"get td1 return value error");
printf("Thread1 return value %ld\n",ret_ptr);
err = pthread_join(td2,&ret_ptr);
if (err != 0)
err_exit(err,"get td2 return value error");
printf("Thread2 return value %ld\n",ret_ptr);
exit(0);
}
~
~
~
~
~
~
~
~
编译及运行结果:
gcc -Wall -ggdb3 11_3.c error.c -o get_ret_value -lpthread
./get_ret_value
Thread 1 is runing
Thread 2 is runing
Thread1 return value 1
Thread2 return value 2