1pthread_join(3)            Library Functions Manual            pthread_join(3)
2
3
4

NAME

6       pthread_join - join with a terminated thread
7

LIBRARY

9       POSIX threads library (libpthread, -lpthread)
10

SYNOPSIS

12       #include <pthread.h>
13
14       int pthread_join(pthread_t thread, void **retval);
15

DESCRIPTION

17       The pthread_join() function waits for the thread specified by thread to
18       terminate.  If that thread has already terminated, then  pthread_join()
19       returns immediately.  The thread specified by thread must be joinable.
20
21       If  retval  is  not NULL, then pthread_join() copies the exit status of
22       the target thread (i.e., the value that the target thread  supplied  to
23       pthread_exit(3)) into the location pointed to by retval.  If the target
24       thread was canceled, then PTHREAD_CANCELED is placed  in  the  location
25       pointed to by retval.
26
27       If  multiple  threads  simultaneously try to join with the same thread,
28       the results are undefined.  If the  thread  calling  pthread_join()  is
29       canceled,  then  the  target thread will remain joinable (i.e., it will
30       not be detached).
31

RETURN VALUE

33       On success, pthread_join() returns 0; on error,  it  returns  an  error
34       number.
35

ERRORS

37       EDEADLK
38              A  deadlock  was  detected (e.g., two threads tried to join with
39              each other); or thread specifies the calling thread.
40
41       EINVAL thread is not a joinable thread.
42
43       EINVAL Another thread is already waiting to join with this thread.
44
45       ESRCH  No thread with the ID thread could be found.
46

ATTRIBUTES

48       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
49       tributes(7).
50
51       ┌────────────────────────────────────────────┬───────────────┬─────────┐
52Interface                                   Attribute     Value   
53       ├────────────────────────────────────────────┼───────────────┼─────────┤
54pthread_join()                              │ Thread safety │ MT-Safe │
55       └────────────────────────────────────────────┴───────────────┴─────────┘
56

STANDARDS

58       POSIX.1-2008.
59

HISTORY

61       POSIX.1-2001.
62

NOTES

64       After  a  successful  call  to pthread_join(), the caller is guaranteed
65       that the target thread has terminated.  The caller may then  choose  to
66       do any clean-up that is required after termination of the thread (e.g.,
67       freeing memory or other resources that were  allocated  to  the  target
68       thread).
69
70       Joining  with a thread that has previously been joined results in unde‐
71       fined behavior.
72
73       Failure to join with a thread that is joinable (i.e., one that  is  not
74       detached),  produces  a  "zombie thread".  Avoid doing this, since each
75       zombie thread consumes some system resources, and  when  enough  zombie
76       threads  have  accumulated, it will no longer be possible to create new
77       threads (or processes).
78
79       There is no pthreads analog of waitpid(-1, &status, 0), that is,  "join
80       with any terminated thread".  If you believe you need this functionali‐
81       ty, you probably need to rethink your application design.
82
83       All of the threads in a process are peers: any thread can join with any
84       other thread in the process.
85

EXAMPLES

87       See pthread_create(3).
88

SEE ALSO

90       pthread_cancel(3),  pthread_create(3),  pthread_detach(3),  pthread_ex‐
91       it(3), pthread_tryjoin_np(3), pthreads(7)
92
93
94
95Linux man-pages 6.04              2023-03-30                   pthread_join(3)
Impressum