]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/pthread_join.3
fork.2: SEE ALSO: add daemon(3)
[thirdparty/man-pages.git] / man3 / pthread_join.3
CommitLineData
ebdd7ee1
MK
1.\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2.\" <mtk.manpages@gmail.com>
3.\"
4.\" Permission is granted to make and distribute verbatim copies of this
5.\" manual provided the copyright notice and this permission notice are
6.\" preserved on all copies.
7.\"
8.\" Permission is granted to copy and distribute modified versions of this
9.\" manual under the conditions for verbatim copying, provided that the
10.\" entire resulting derived work is distributed under the terms of a
11.\" permission notice identical to this one.
12.\"
13.\" Since the Linux kernel and libraries are constantly changing, this
14.\" manual page may be incorrect or out-of-date. The author(s) assume no
15.\" responsibility for errors or omissions, or for damages resulting from
16.\" the use of the information contained herein. The author(s) may not
17.\" have taken the same level of care in the production of this manual,
18.\" which is licensed free of charge, as they might when working
19.\" professionally.
20.\"
21.\" Formatted or processed versions of this manual, if unaccompanied by
22.\" the source, must acknowledge the copyright and authors of this work.
23.\"
48718eb3 24.TH PTHREAD_JOIN 3 2008-11-27 "Linux" "Linux Programmer's Manual"
ebdd7ee1
MK
25.SH NAME
26pthread_join \- join with a terminated thread
27.SH SYNOPSIS
28.nf
29.B #include <pthread.h>
30
31.BI "int pthread_join(pthread_t " thread ", void **" retval );
32.fi
33.sp
34Compile and link with \fI\-pthread\fP.
35.SH DESCRIPTION
36The
37.BR pthread_join ()
38function waits for the thread specified by
39.IR thread
40to terminate.
41If that thread has already terminated, then
42.BR pthread_join ()
43returns immediately.
44The thread specified by
45.I thread
46must be joinable.
47
48If
49.I retval
50is not NULL, then
51.BR pthread_join ()
52copies the exit status of the target thread
53(i.e., the value that the target thread supplied to
54.BR pthread_exit (3))
55into the location pointed to by
56.IR *retval .
57If the target thread was canceled, then
58.B PTHREAD_CANCELED
59is placed in
60.IR *retval .
61
62If multiple threads simultaneously try to join with the same thread,
63the results are undefined.
64If the thread calling
65.BR pthread_join ()
66is canceled, then the target thread will remain joinable
67(i.e., it will not be detached).
68.SH RETURN VALUE
69On success,
70.BR pthread_join ()
71returns 0;
72on error, it returns an error number.
73.SH ERRORS
74.TP
75.B EDEADLK
76A deadlock was detected
77.\" The following verified by testing on glibc 2.8/NPTL:
78(e.g., two threads tried to join with each other);
79or
80.\" The following verified by testing on glibc 2.8/NPTL:
81.I thread
82specifies the calling thread.
83.TP
84.B EINVAL
85.I thread
86is not a joinable thread.
bbb08de3
MK
87.TP
88.B EINVAL
89Another thread is already waiting to join with this thread.
90.\" POSIX.1-2001 does not specify this error case.
ebdd7ee1
MK
91.TP
92.B ESRCH
48718eb3
MK
93No thread with the ID
94.I thread
95could be found.
ebdd7ee1
MK
96.SH CONFORMING TO
97POSIX.1-2001.
98.SH NOTES
99After a successful call to
100.BR pthread_create (),
101the caller is guaranteed that the target thread has terminated.
102
103Joining with a thread that has previously been joined results in
240c2fa0 104undefined behavior.
ebdd7ee1
MK
105
106Failure to join with a thread that is joinable
107(i.e., one that is not detached),
108produces a "zombie thread".
109Avoid doing this,
110since each zombie thread consumes some system resources,
111and when enough zombie threads have accumulated,
112it will no longer be possible to create new threads (or processes).
113
114There is no pthreads analog of
115.IR "waitpid(-1,\ &status,\ 0)" ,
116that is, "join with any terminated thread".
117If you believe you need this functionality,
118you probably need to rethink your application design.
119
120All of the threads in a process are peers:
121any thread can join with any other thread in the process.
122.SH EXAMPLE
123See
124.BR pthread_create (3).
125.SH SEE ALSO
126.BR pthread_cancel (3),
127.BR pthread_create (3),
128.BR pthread_detach (3),
129.BR pthread_exit (3),
43fa988b 130.BR pthread_tryjoin_np (3).
ebdd7ee1 131.BR pthreads (7)