POSIX多线程编程(二)--pthread_join
来源:互联网 发布:在线排课软件 编辑:程序博客网 时间:2024/05/22 14:13
To explicitly create a thread as joinable or detached, the attr argument in the pthread_create() routine is used. The typical 4 step process is:
- Declare a pthread attribute variable of the pthread_attr_t data type
- Initialize the attribute variable with pthread_attr_init()
- Set the attribute detached status with pthread_attr_setdetachstate()
- When done, free library resources used by the attribute with pthread_attr_destroy()
/** * NAME * pthread_join - wait for thread termination * SYNOPSIS * #include <pthread.h> * int pthread_join(pthread_t thread, void **value_ptr); * DESCRIPTION * The pthread_join() function shall suspend execution of the calling * thread until the target thread terminates, unless the target thread has * already terminated. On return from a successful pthread_join() call * with a non-NULL value_ptr argument, the value passed to pthread_exit() * by the terminating thread shall be made available in the location ref- * erenced by value_ptr. When a pthread_join() returns successfully, the * target thread has been terminated. The results of multiple simultaneous * calls to pthread_join() specifying the same target thread are unde- * fined. If the thread calling pthread_join() is canceled, then the tar- * get thread shall not be detached. * It is unspecified whether a thread that has exited but remains unjoined * counts against {PTHREAD_THREADS_MAX}. * RETURN VALUE * If successful, the pthread_join() function shall return zero; other- * wise, an error number shall be returned to indicate the error. */#include <pthread.h>#include <stdio.h>#include <stdlib.h>#define NUM_THREADS4//线程执行的函数void *BusyWork(void *t){int i;long tid;double result = 0.0;tid = (long) t;printf("Thread %ld starting...\n", tid);for (i = 0; i < 1000000; i++){result = result + sin(i) * tan(i);}printf("Thread %ld done. Result = %e\n", tid, result);pthread_exit((void*) t);return NULL ;}int main(int argc, char *argv[]){pthread_t thread[NUM_THREADS];//定义pthread_attr_t变量pthread_attr_t attr;int rc;long t;void *status;/* Initialize and set thread detached attribute *//** * 主要注意一下两个方法 * 初始化attr * pthread_attr_init * 设置这个线程是可以joinable * pthread_attr_setdetachstate */pthread_attr_init(&attr);pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);for (t = 0; t < NUM_THREADS; t++){printf("Main: creating thread %ld\n", t);//主要是函数的第二个参数//把设置好的attr传入rc = pthread_create(&thread[t], &attr, BusyWork, (void *) t);if (rc){printf("ERROR; return code from pthread_create() is %d\n", rc);exit(-1);}}/* Free attribute and wait for the other threads *///释放attributepthread_attr_destroy(&attr);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");pthread_exit(NULL );}
- POSIX多线程编程(二)--pthread_join
- POSIX多线程编程(二)
- 《posix多线程编程》笔记(二)
- Posix多线程编程学习笔记(二)
- Linux 多线程编程( POSIX )( 二 )----->代码区
- 多线程编程学习::POSIX 多线程基础(二)
- POSIX 多线程 编程
- POSIX多线程编程
- Posix多线程编程
- Posix多线程编程整理
- Posix多线程编程指南
- POSIX多线程编程
- Posix thread 多线程编程
- POSIX多线程编程
- posix多线程编程
- POSIX多线程编程(一)
- Posix thread 多线程编程
- POSIX多线程编程概述
- 最近做了一个zencart做的卖鞋子的外贸网站
- 分享[sf]上发现的一个别人的php中文分词库
- oracle存储过程
- 西藏近5个月缴获枪支966支子弹3万余发-西藏-枪支-子弹
- Vista 驱动架构 ATI催化剂细节
- POSIX多线程编程(二)--pthread_join
- hdu 2059 龟兔赛跑 动规问题
- Android开发环境搭建中一些问题的解决方案
- 第四章 运算符、表达式和语句
- Maven基础
- 输出三角形星号图第一种
- hduoj1047,Integer Inquiry,大数加法,跟poj某一题一样,再谈
- 操作系统 图形窗口 子系统
- QStyle的使用及范例