]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/pthread_join.3
man*/: ffix (un-bracket tables)
[thirdparty/man-pages.git] / man3 / pthread_join.3
CommitLineData
a1eaacb1 1'\" t
ebdd7ee1
MK
2.\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
3.\" <mtk.manpages@gmail.com>
4.\"
5fbde956 5.\" SPDX-License-Identifier: Linux-man-pages-copyleft
ebdd7ee1 6.\"
4c1c5274 7.TH pthread_join 3 (date) "Linux man-pages (unreleased)"
ebdd7ee1
MK
8.SH NAME
9pthread_join \- join with a terminated thread
574dfc58
AC
10.SH LIBRARY
11POSIX threads library
8fc3b2cf 12.RI ( libpthread ", " \-lpthread )
ebdd7ee1
MK
13.SH SYNOPSIS
14.nf
15.B #include <pthread.h>
dbfe9c70 16.PP
ebdd7ee1
MK
17.BI "int pthread_join(pthread_t " thread ", void **" retval );
18.fi
ebdd7ee1
MK
19.SH DESCRIPTION
20The
21.BR pthread_join ()
22function waits for the thread specified by
1ae6b2c7 23.I thread
ebdd7ee1
MK
24to terminate.
25If that thread has already terminated, then
26.BR pthread_join ()
27returns immediately.
28The thread specified by
29.I thread
30must be joinable.
847e0d88 31.PP
ebdd7ee1
MK
32If
33.I retval
34is not NULL, then
35.BR pthread_join ()
36copies the exit status of the target thread
37(i.e., the value that the target thread supplied to
38.BR pthread_exit (3))
39into the location pointed to by
919b0ce0 40.IR retval .
ebdd7ee1
MK
41If the target thread was canceled, then
42.B PTHREAD_CANCELED
919b0ce0
MK
43is placed in the location pointed to by
44.IR retval .
847e0d88 45.PP
ebdd7ee1
MK
46If multiple threads simultaneously try to join with the same thread,
47the results are undefined.
48If the thread calling
49.BR pthread_join ()
50is canceled, then the target thread will remain joinable
51(i.e., it will not be detached).
52.SH RETURN VALUE
53On success,
54.BR pthread_join ()
55returns 0;
56on error, it returns an error number.
57.SH ERRORS
58.TP
59.B EDEADLK
60A deadlock was detected
61.\" The following verified by testing on glibc 2.8/NPTL:
62(e.g., two threads tried to join with each other);
63or
64.\" The following verified by testing on glibc 2.8/NPTL:
65.I thread
66specifies the calling thread.
67.TP
68.B EINVAL
69.I thread
70is not a joinable thread.
bbb08de3
MK
71.TP
72.B EINVAL
73Another thread is already waiting to join with this thread.
74.\" POSIX.1-2001 does not specify this error case.
ebdd7ee1
MK
75.TP
76.B ESRCH
48718eb3
MK
77No thread with the ID
78.I thread
79could be found.
acc271be
ZL
80.SH ATTRIBUTES
81For an explanation of the terms used in this section, see
82.BR attributes (7).
83.TS
84allbox;
c466875e 85lbx lb lb
acc271be
ZL
86l l l.
87Interface Attribute Value
88T{
9e54434e
BR
89.na
90.nh
acc271be
ZL
91.BR pthread_join ()
92T} Thread safety MT-Safe
93.TE
847e0d88 94.sp 1
3113c7f3 95.SH STANDARDS
4131356c
AC
96POSIX.1-2008.
97.SH HISTORY
98POSIX.1-2001.
ebdd7ee1
MK
99.SH NOTES
100After a successful call to
c162db87 101.BR pthread_join (),
ebdd7ee1 102the caller is guaranteed that the target thread has terminated.
46305699
MK
103The caller may then choose to do any clean-up that is required
104after termination of the thread (e.g., freeing memory or other
105resources that were allocated to the target thread).
847e0d88 106.PP
ebdd7ee1 107Joining with a thread that has previously been joined results in
240c2fa0 108undefined behavior.
847e0d88 109.PP
ebdd7ee1
MK
110Failure to join with a thread that is joinable
111(i.e., one that is not detached),
112produces a "zombie thread".
113Avoid doing this,
114since each zombie thread consumes some system resources,
115and when enough zombie threads have accumulated,
116it will no longer be possible to create new threads (or processes).
847e0d88 117.PP
ebdd7ee1 118There is no pthreads analog of
cd415e73 119.IR "waitpid(\-1,\ &status,\ 0)" ,
ebdd7ee1
MK
120that is, "join with any terminated thread".
121If you believe you need this functionality,
122you probably need to rethink your application design.
847e0d88 123.PP
ebdd7ee1
MK
124All of the threads in a process are peers:
125any thread can join with any other thread in the process.
a14af333 126.SH EXAMPLES
ebdd7ee1
MK
127See
128.BR pthread_create (3).
129.SH SEE ALSO
130.BR pthread_cancel (3),
131.BR pthread_create (3),
132.BR pthread_detach (3),
133.BR pthread_exit (3),
33a5ffc9 134.BR pthread_tryjoin_np (3),
ebdd7ee1 135.BR pthreads (7)