pthread_join/pthread_exit
2012-12-11 13:17
288 查看
转自:http://www.cnblogs.com/stli/archive/2010/03/11/1683808.html
函数pthread_join用来等待一个线程的结束。函数原型为:
extern int pthread_join __P ((pthread_t __th, void **__thread_return));
第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。这个函数是一个线程阻塞的函数,调用它的线程将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。
一个线程的结束有两种途径,一种是像我们上面的例子一样,函数结束了,调用它的线程也就结束了;另一种方式是通过函数pthread_exit来实现。它的函数原型为:
extern void pthread_exit __P ((void *__retval)) __attribute__ ((__noreturn__));
唯一的参数是函数的返回代码,只要pthread_exit中的参数retval不是NULL,这个值将被传递给 thread_return。
最后要说明的是,一个线程不能被多个线程等待,也就是说不能在多个线程中调用参数th为同一个线程标识符的pthread_join,否则第一个调用pthread_join的线程接收到信号成功返回,其余调用pthread_join的线程则返回错误代码ESRCH。
实例:
/*myfile11-3.c*/
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
pthread_t tid1, tid2;
void *tret;
void *
thr_fn1(void *arg)
{
sleep(1);//睡眠一秒,等待TID2结束。
pthread_join(tid2, &tret);//tid1一直阻赛,等到tid2的退出,获得TID2的退出码
printf("thread 2 exit code %d\n", (int)tret);
printf("thread 1 returning\n");
return((void *)2);
}
void *
thr_fn2(void *arg)
{
printf("thread 2 exiting\n");
pthread_exit((void *)3);
}
int
main(void)
{
int err;
err = pthread_create(&tid1, NULL, thr_fn1, NULL);
if (err != 0)
printf("can't create thread 1\n");
err = pthread_create(&tid2, NULL, thr_fn2, NULL);
if (err != 0)
printf("can't create thread 2\n");
err = pthread_join(tid1, &tret);//线程一直阻赛,等待TID1的返回。
if (err != 0)
printf("can't join with thread 1\n");
printf("thread 1 exit code %d\n", (int)tret);
//err = pthread_join(tid2, &tret);
//if (err != 0)
// printf("can't join with thread 2\n");
// printf("thread 2 exit code %d\n", (int)tret);
exit(0);
}
命令:#gcc -lthread myfile11-3.c
:#./a.out
运行结果:
thread 2 exiting
thread 2 exit code 3
thread 1 returning
thread 1 exit code 2
函数pthread_join用来等待一个线程的结束。函数原型为:
extern int pthread_join __P ((pthread_t __th, void **__thread_return));
第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。这个函数是一个线程阻塞的函数,调用它的线程将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。
一个线程的结束有两种途径,一种是像我们上面的例子一样,函数结束了,调用它的线程也就结束了;另一种方式是通过函数pthread_exit来实现。它的函数原型为:
extern void pthread_exit __P ((void *__retval)) __attribute__ ((__noreturn__));
唯一的参数是函数的返回代码,只要pthread_exit中的参数retval不是NULL,这个值将被传递给 thread_return。
最后要说明的是,一个线程不能被多个线程等待,也就是说不能在多个线程中调用参数th为同一个线程标识符的pthread_join,否则第一个调用pthread_join的线程接收到信号成功返回,其余调用pthread_join的线程则返回错误代码ESRCH。
实例:
/*myfile11-3.c*/
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
pthread_t tid1, tid2;
void *tret;
void *
thr_fn1(void *arg)
{
sleep(1);//睡眠一秒,等待TID2结束。
pthread_join(tid2, &tret);//tid1一直阻赛,等到tid2的退出,获得TID2的退出码
printf("thread 2 exit code %d\n", (int)tret);
printf("thread 1 returning\n");
return((void *)2);
}
void *
thr_fn2(void *arg)
{
printf("thread 2 exiting\n");
pthread_exit((void *)3);
}
int
main(void)
{
int err;
err = pthread_create(&tid1, NULL, thr_fn1, NULL);
if (err != 0)
printf("can't create thread 1\n");
err = pthread_create(&tid2, NULL, thr_fn2, NULL);
if (err != 0)
printf("can't create thread 2\n");
err = pthread_join(tid1, &tret);//线程一直阻赛,等待TID1的返回。
if (err != 0)
printf("can't join with thread 1\n");
printf("thread 1 exit code %d\n", (int)tret);
//err = pthread_join(tid2, &tret);
//if (err != 0)
// printf("can't join with thread 2\n");
// printf("thread 2 exit code %d\n", (int)tret);
exit(0);
}
命令:#gcc -lthread myfile11-3.c
:#./a.out
运行结果:
thread 2 exiting
thread 2 exit code 3
thread 1 returning
thread 1 exit code 2
相关文章推荐
- Linux下多线程编程__线程的创建pthread_create与退出pthread_exit,等待当前线程退出pthread_join,获取线程ID pthread_self
- pthread_join/pthread_exit用法实例
- 线程终止thread_exit,pthread_cancel,pthread_join
- 笔记十:线程间的通信(pthread_exit()和pthread_join())
- pthread_exit(void *rval_ptr)和pthread_join(pthread_t thread, void **rval_ptr)
- 线程正常终止pthread_exit,pthread_join,pthread_kill,pthread_cancel,sigwait,sigaddset
- Linux线程pthread_exit 和 pthread_join
- Linux多线程实例练习 - pthread_exit() 与 pthread_join()
- 多线程函数系列pthread_create(), pthread_join(), pthread_self(),pthread_exit(), pthread_detach()实例详解
- 线程pthread_create()、pthread_exit()、pthread_join()、pthread_cancel()
- pthread_join/pthread_exit用法实例
- pthread_join/pthread_exit用法实例
- pthread_join/pthread_exit用法实例
- pthread_join/pthread_exit的用法解析
- 多线程:pthread_exit,pthread_join,pthread_self
- pthread_join/pthread_exit用法实例
- linux 线程等待与退出 pthread_join pthread_exit 函数
- 多线程:pthread_exit,pthread_join,pthread_self
- pthread_join/pthread_exit的使用方法解析
- linux c之使用pthread_create创建线程pthread_join等待线程和pthread_exit终止线程总结