pthread_join
2015-12-30 11:02
253 查看
需要注意:所使用的存储返回信息的结构,在调用者完成调用以后,必须仍然有效。
实例程序:
#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
实例程序:
#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
相关文章推荐
- linux C 线程编程
- pthread_create函数 pthread_self 函数
- pthread_create和timeval不能同时使用
- UNIX环境高级编程(第11章 线程)
- UNIX环境高级编程(第12章 线程控制)
- APUE-线程:pthread_create的实现与apue2的区别
- C++使用pthread_create()
- Unix高级环境编程学习之-----线程pthread_create
- linux 线程创建函数pthread_create的几个传参方式
- pthread_create 用法多线程文件拷贝。
- pthread_create 函数引发的几点思考
- ubuntu 12.04 codeblocks undefined reference to ‘pthread_create'解决方法
- 网络编程(6)单进程多线程并发服务器实现
- 类成员函数作线程的启动函数参数
- 线程
- pthread_create 参数传递指针问题
- 线程创建 pthread_create 中自定义参数注意事项
- 初识Linux多线程编程
- linux下的线程的创建退出及回收
- NuttX下实现周期任务管理服务