PTHREAD_JOIN(3P)
π PTHREAD_JOIN(3P) β POSIX Programmer's Manual π
okay bestie, quick vibe check before we dive in: this page lives in the POSIX Programmer's Manual. That's the group chat, and the Linux implementation of this interface may be different (so pls go consult the corresponding Linux manual page for the deets on Linux behavior) β or honestly, the interface might just not be implemented on Linux at all. no cap. π«Ά
β¨ PROLOG β¨
so this manual page is part of the POSIX Programmer's Manual, periodt. The Linux implementation of this interface may differ β go read the corresponding Linux manual page for details on Linux behavior β or the interface may simply not be implemented on Linux. either way, we ball. π
π NAME π
pthread_join β wait for thread termination
πΈ SYNOPSIS πΈ
#include <pthread.h>
int pthread_join(pthread_t thread, void **value_ptr);
π¦ DESCRIPTION π¦
okay so here's the tea. The pthread_join() function shall suspend execution of the calling thread until the target thread terminates β unless the target thread has already terminated, in which case, girl, you're already done. π
On return from a successful pthread_join() call with a non-NULL value_ptr argument, the value that the terminating thread passed to pthread_exit() shall be made available in the location referenced by value_ptr. so like, the thread hands you its little goodbye gift through that pointer. π
When a pthread_join() returns successfully, the target thread has been terminated. like, fully. gone. slayed.
β οΈ The results of multiple simultaneous calls to pthread_join() specifying the same target thread are undefined. do not do this. i'm literally begging. π
If the thread calling pthread_join() is canceled, then the target thread shall not be detached. important!!! β¨
It is unspecified whether a thread that has exited but remains unjoined counts against {PTHREAD_THREADS_MAX}. like, nobody knows, it's giving mystery. π
π¨ The behavior is undefined if the value specified by thethreadargument topthread_join()does not refer to a joinable thread.
π¨ The behavior is undefined if the value specified by thethreadargument topthread_join()refers to the calling thread. that's you joining yourself. main character behavior gone wrong. no. π
π RETURN VALUE π
If successful, the pthread_join() function shall return zero β cute, clean, iconic. β¨
Otherwise, an error number shall be returned to indicate the error. not a -1, bestie, an error number. iykyk.
π ERRORS π
The pthread_join() function may fail if:
EDEADLKβ A deadlock was detected. π (yeah, it's giving deadlock, and it's giving detected.)
π¨ Thepthread_join()function shall not return an error code of[EINTR]. don't even ask, it's just not happening.
The following sections are informative.
π₯ EXAMPLES π₯
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;
}
two threads, two halves of the array, both incremented, both joined. the way that this just works. ate. π
π§ APPLICATION USAGE π§
None. literally nothing. empty. the void. π³οΈβ¨
π RATIONALE π
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β2017 was considered valuable. periodt. π
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. so like, it's the cleanup arc. π§Ή
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. (yes, every thread. don't leave them hanging. that's rude. π₯Ί)
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. iconic, honestly. π
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.
β FUTURE DIRECTIONS β
None. the future is empty, bestie. nothing to see here. π¦
π SEE ALSO π
pthread_create(),wait()- The Base Definitions volume of POSIX.1β2017, Section 4.12, Memory Synchronization,
<pthread.h>
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie!! this one's brought to you by IEEE/The Open Group, dated 2017, filed under PTHREAD_JOIN(3P) β go forth and join your threads, but like, only once each, okay? mwah ππβ¨
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms