POSIX的pthread_join
来源:互联网 发布:阿里云还是腾讯云 编辑:程序博客网 时间:2024/06/05 05:44
http://blog.csdn.net/ba_jie/article/details/6783205
函数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。最后要说明的是,一个线程不能被多个线程等待,否则第一个接收到信号的线程成功返回,其余调用pthread_join的线程则返回错误代码ESRCH。
join
- join是三种同步线程的方式之一。另外两种分别是互斥锁(mutex)和条件变量(condition variable)。
- 调用pthread_join()将阻塞自己,一直到要等待加入的线程运行结束。
- 可以用pthread_join()获取线程的返回值。
- 一个线程对应一个pthread_join()调用,对同一个线程进行多次pthread_join()调用是逻辑错误。
join or detach
- 线程分两种:一种可以join,另一种不可以。该属性在创建线程的时候指定。
- joinable线程可在创建后,用pthread_detach()显式地分离。但分离后不可以再合并。该操作不可逆。
- 为了确保移植性,在创建线程时,最好显式指定其join或detach属性。似乎不是所有POSIX实现都是用joinable作默认。
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define NUM_THREADS 4
- void *BusyWork(void *t)
- {
- double result=0.0;
- long tid = (long)t;
- printf("Thread %ld starting...\n",tid);
- for (int i=0; i<1000000; i++)
- {
- result = result + sin(i) * tan(i);
- }
- printf("Thread %ld done. Result = %e\n",tid, result);
- pthread_exit((void*) t);
- }
- int main (int argc, char *argv[])
- {
- pthread_t thread[NUM_THREADS];
- pthread_attr_t attr;
- // 1/4: init
- pthread_attr_init(&attr);
- // 2/4: explicitly specify as joinable or detached
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
- int rc;
- long t;
- for(t=0; t<NUM_THREADS; t++)
- {
- printf("Main: creating thread %ld\n", t);
- // 3/4: use thread attribute
- rc = pthread_create(&thread[t], &attr, BusyWork, (void *)t);
- if (rc) {
- printf("ERROR; return code from pthread_create() is %d\n", rc);
- exit(-1);
- }
- }
- // 4/4: release thread attribute
- pthread_attr_destroy(&attr);
- void *status;
- for(t=0; t<NUM_THREADS; t++)
- {
- rc = pthread_join(thread[t], &status);
- if (rc) {
- printf("ERROR; return code from pthread_join() is %d\n", rc);
- exit(-1);
- }
- printf("Main: completed join with thread %ld having a status of %ld\n",t,(long)status);
- }
- printf("Main: program completed. Exiting.\n");
- return 0;
- }
0 0
- POSIX的pthread_join
- POSIX的pthread_join
- POSIX的pthread_join
- POSIX的pthread_join
- linux posix 通过pthread_join获取线程pthread_exit返回的数据
- POSIX多线程编程(二)--pthread_join
- POSIX pthread_join pthread_detach小验证
- POSIX的pthread_join http://blog.csdn.net/ba_jie/article/details/6783205
- POSIX线程,线程的客户/服务通信(pthread_join,pthread_exit,pthread_detach,pthread_self)
- 神奇的pthread_join
- pthread_join的应用
- pthread_join的使用方法
- pthread_join的段错误
- pthread_join的使用
- pthread_join
- pthread_join
- pthread_join
- pthread_join
- tomcat运行framework7
- POJ 3714 Raid(最近点对)
- 第一篇文章
- Linux下core文件产生的一些注意问题
- ACM--steps--4.3.7--Sequence one
- POSIX的pthread_join
- Android 实用工具Hierarchy Viewer实战
- 在oracle存储过程中创建临时表
- java 初识
- TCP协议中的三次握手和四次挥手(图解)
- R语言学习——2.数据输入
- js模板引擎介绍搜集
- 容器
- BZOJ 3112 Zjoi2013 防守战线 单纯形