1PTHREAD_JOIN(3) Linux Programmer's Manual PTHREAD_JOIN(3)
2
3
4
6 pthread_join - join with a terminated thread
7
9 #include <pthread.h>
10
11 int pthread_join(pthread_t thread, void **retval);
12
13 Compile and link with -pthread.
14
16 The pthread_join() function waits for the thread specified by thread to
17 terminate. If that thread has already terminated, then pthread_join()
18 returns immediately. The thread specified by thread must be joinable.
19
20 If retval is not NULL, then pthread_join() copies the exit status of
21 the target thread (i.e., the value that the target thread supplied to
22 pthread_exit(3)) into the location pointed to by retval. If the target
23 thread was canceled, then PTHREAD_CANCELED is placed in the location
24 pointed to by retval.
25
26 If multiple threads simultaneously try to join with the same thread,
27 the results are undefined. If the thread calling pthread_join() is
28 canceled, then the target thread will remain joinable (i.e., it will
29 not be detached).
30
32 On success, pthread_join() returns 0; on error, it returns an error
33 number.
34
36 EDEADLK
37 A deadlock was detected (e.g., two threads tried to join with
38 each other); or thread specifies the calling thread.
39
40 EINVAL thread is not a joinable thread.
41
42 EINVAL Another thread is already waiting to join with this thread.
43
44 ESRCH No thread with the ID thread could be found.
45
47 For an explanation of the terms used in this section, see
48 attributes(7).
49
50 ┌───────────────┬───────────────┬─────────┐
51 │Interface │ Attribute │ Value │
52 ├───────────────┼───────────────┼─────────┤
53 │pthread_join() │ Thread safety │ MT-Safe │
54 └───────────────┴───────────────┴─────────┘
55
57 POSIX.1-2001, POSIX.1-2008.
58
60 After a successful call to pthread_join(), the caller is guaranteed
61 that the target thread has terminated. The caller may then choose to
62 do any clean-up that is required after termination of the thread (e.g.,
63 freeing memory or other resources that were allocated to the target
64 thread).
65
66 Joining with a thread that has previously been joined results in unde‐
67 fined behavior.
68
69 Failure to join with a thread that is joinable (i.e., one that is not
70 detached), produces a "zombie thread". Avoid doing this, since each
71 zombie thread consumes some system resources, and when enough zombie
72 threads have accumulated, it will no longer be possible to create new
73 threads (or processes).
74
75 There is no pthreads analog of waitpid(-1, &status, 0), that is, "join
76 with any terminated thread". If you believe you need this functional‐
77 ity, you probably need to rethink your application design.
78
79 All of the threads in a process are peers: any thread can join with any
80 other thread in the process.
81
83 See pthread_create(3).
84
86 pthread_cancel(3), pthread_create(3), pthread_detach(3),
87 pthread_exit(3), pthread_tryjoin_np(3), pthreads(7)
88
90 This page is part of release 4.16 of the Linux man-pages project. A
91 description of the project, information about reporting bugs, and the
92 latest version of this page, can be found at
93 https://www.kernel.org/doc/man-pages/.
94
95
96
97Linux 2017-09-15 PTHREAD_JOIN(3)