pthread_join — wait for thread termination
#include <pthread.h>
int pthread_join(pthread_t thread, void **value_ptr);
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 referenced 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 undefined. If the thread calling pthread_join() is canceled, then the target thread shall not be detached.
It is unspecified whether a zombie thread counts against {PTHREAD_THREADS_MAX}.
The behavior is undefined if the value specified by the thread argument to pthread_join() does not refer to a joinable thread.
The behavior is undefined if the value specified by the thread argument to pthread_join() refers to the calling thread.
If thread refers to a thread that was created using thrd_create() and the thread terminates, or has already terminated, by returning from its start routine, the behavior of pthread_join() is undefined. If thread refers to a thread that terminates, or has already terminated, by calling thrd_exit(), the behavior of pthread_join() is undefined.
If successful, the pthread_join() function shall return zero; otherwise, an error number shall be returned to indicate the error.
The pthread_join() function may fail if:
- [EDEADLK]
- A deadlock was detected.
The pthread_join() function shall not return an error code of [EINTR].
An example of thread creation and deletion follows:
typedef struct { int *ar; long n; } subarray;
void * incer(void *arg) { long i;
for (i = 0; i < ((subarray *)arg)->n; i++) ((subarray *)arg)->ar[i]++; }
int main(void) { int ar[1000000]; pthread_t th1, th2; subarray sb1, sb2;
sb1.ar = &ar[0]; sb1.n = 500000; (void) pthread_create(&th1, NULL, incer, &sb1);
sb2.ar = &ar[500000]; sb2.n = 500000; (void) pthread_create(&th2, NULL, incer, &sb2);
(void) pthread_join(th1, NULL); (void) pthread_join(th2, NULL); return 0; }
None.
The pthread_join() function is a convenience that has proven useful in multi-threaded applications. It is true that a programmer could simulate this function if it were not provided by passing extra state as part of the argument to the start_routine(). The terminating thread would set a flag to indicate termination and broadcast a condition that is part of that state; a joining thread would wait on that condition variable. While such a technique would allow a thread to wait on more complex conditions (for example, waiting for multiple threads to terminate), waiting on individual thread termination is considered widely useful. Also, including the pthread_join() function in no way precludes a programmer from coding such complex waits. Thus, while not a primitive, including pthread_join() in this volume of POSIX.1-2024 was considered valuable.
The pthread_join() function provides a simple mechanism allowing an application to wait for a thread to terminate. After the thread terminates, the application may then choose to clean up resources that were used by the thread. For instance, after pthread_join() returns, any application-provided stack storage could be reclaimed.
The pthread_join() or pthread_detach() function should eventually be called for every thread that is created with the detachstate attribute set to PTHREAD_CREATE_JOINABLE so that storage associated with the thread may be reclaimed.
The interaction between pthread_join() and cancellation is well-defined for the following reasons:
The pthread_join() function, like all other non-async-cancel-safe functions, can only be called with deferred cancelability type.
Cancellation cannot occur in the disabled cancelability state.
Thus, only the default cancelability state need be considered. As specified, either the pthread_join() call is canceled, or it succeeds, but not both. The difference is obvious to the application, since either a cancellation handler is run or pthread_join() returns. There are no race conditions since pthread_join() was called in the deferred cancelability state.
If an implementation detects that the value specified by the thread argument to pthread_join() does not refer to a joinable thread, it is recommended that the function should fail and report an [EINVAL] error.
If an implementation detects that the value specified by the thread argument to pthread_join() refers to the calling thread, it is recommended that the function should fail and report an [EDEADLK] error.
If an implementation detects use of a thread ID after the end of its lifetime, it is recommended that the function should fail and report an [ESRCH] error.
The pthread_join() function cannot be used to obtain the exit status of a thread that was created using thrd_create() and which terminates by returning from its start routine, or of a thread that terminates by calling thrd_exit(), because such threads have an int exit status, instead of the void * that pthread_join() returns via its value_ptr argument.
None.
First released in Issue 5. Included for alignment with the POSIX Threads Extension.
The pthread_join() function is marked as part of the Threads option.
IEEE Std 1003.1-2001/Cor 2-2004, item XSH/TC2/D6/97 is applied, updating the ERRORS section so that the [EINVAL] error is made optional and the words "the implementation has detected" are removed from it.
The pthread_join() function is moved from the Threads option to the Base.
Austin Group Interpretation 1003.1-2001 #142 is applied, removing the [ESRCH] error condition.
The [EINVAL] error for a non-joinable thread is removed; this condition results in undefined behavior.
The [EDEADLK] error for the calling thread is removed; this condition results in undefined behavior.
Austin Group Defect 792 is applied, changing "a thread that has exited but remains unjoined" to "a zombie thread".
Austin Group Defect 1302 is applied, updating the page to account for the addition of <threads.h> interfaces.
return to top of page
椴树是什么树 | 爸爸的哥哥叫什么 | 可喜可贺是什么意思 | 香芋是什么 | 马上好药膏主治什么 |
痛风吃什么药止痛最快 | 任督二脉是什么意思 | 男性尿道痒吃什么药 | 溪字五行属什么 | 贫血吃什么补 |
四月初五是什么星座 | 金丝雀是什么意思 | 脑梗都有什么症状 | pr在医学上是什么意思 | 便秘吃什么通便 |
石榴什么时候成熟 | 草长莺飞是什么生肖 | 肺心病吃什么药 | 斯凯奇鞋是什么档次 | 20至30元什么烟最好抽 |
葛粉吃了有什么好处luyiluode.com | 羊水是什么颜色的hcv7jop5ns5r.cn | 为什么尿液一直是黄的hcv9jop3ns1r.cn | 冠脉造影是什么意思hcv8jop9ns0r.cn | 吃月饼是什么生肖hcv8jop5ns2r.cn |
病毒感冒吃什么药hcv9jop2ns2r.cn | 入党有什么好处hcv9jop7ns9r.cn | 胎芽是什么意思hcv8jop3ns1r.cn | 红楼梦结局是什么dayuxmw.com | 玉和翡翠有什么区别hcv8jop7ns6r.cn |
8月5日是什么星座hcv9jop7ns3r.cn | 糖类抗原724偏高是什么原因hcv8jop5ns6r.cn | 流理台是什么hcv9jop3ns8r.cn | 寂是什么意思hcv9jop6ns1r.cn | 破处是什么意思fenrenren.com |
龙鱼是什么鱼hcv7jop9ns6r.cn | nt和唐筛有什么区别hcv8jop0ns4r.cn | 头顶疼是什么原因引起的hcv9jop1ns6r.cn | 耳膜炎是什么原因引起的hcv8jop7ns9r.cn | 什么面料不容易皱hcv8jop2ns3r.cn |