1PTHREAD_DETACH(3) Linux Programmer's Manual PTHREAD_DETACH(3)
2
3
4
6 pthread_detach - detach a thread
7
9 #include <pthread.h>
10
11 int pthread_detach(pthread_t thread);
12
13 Compile and link with -pthread.
14
16 The pthread_detach() function marks the thread identified by thread as
17 detached. When a detached thread terminates, its resources are auto‐
18 matically released back to the system without the need for another
19 thread to join with the terminated thread.
20
21 Attempting to detach an already detached thread results in unspecified
22 behavior.
23
25 On success, pthread_detach() returns 0; on error, it returns an error
26 number.
27
29 EINVAL thread is not a joinable thread.
30
31 ESRCH No thread with the ID thread could be found.
32
34 For an explanation of the terms used in this section, see at‐
35 tributes(7).
36
37 ┌─────────────────┬───────────────┬─────────┐
38 │Interface │ Attribute │ Value │
39 ├─────────────────┼───────────────┼─────────┤
40 │pthread_detach() │ Thread safety │ MT-Safe │
41 └─────────────────┴───────────────┴─────────┘
42
44 POSIX.1-2001, POSIX.1-2008.
45
47 Once a thread has been detached, it can't be joined with
48 pthread_join(3) or be made joinable again.
49
50 A new thread can be created in a detached state using pthread_attr_set‐
51 detachstate(3) to set the detached attribute of the attr argument of
52 pthread_create(3).
53
54 The detached attribute merely determines the behavior of the system
55 when the thread terminates; it does not prevent the thread from being
56 terminated if the process terminates using exit(3) (or equivalently, if
57 the main thread returns).
58
59 Either pthread_join(3) or pthread_detach() should be called for each
60 thread that an application creates, so that system resources for the
61 thread can be released. (But note that the resources of any threads
62 for which one of these actions has not been done will be freed when the
63 process terminates.)
64
66 The following statement detaches the calling thread:
67
68 pthread_detach(pthread_self());
69
71 pthread_attr_setdetachstate(3), pthread_cancel(3), pthread_create(3),
72 pthread_exit(3), pthread_join(3), pthreads(7)
73
75 This page is part of release 5.10 of the Linux man-pages project. A
76 description of the project, information about reporting bugs, and the
77 latest version of this page, can be found at
78 https://www.kernel.org/doc/man-pages/.
79
80
81
82Linux 2020-06-09 PTHREAD_DETACH(3)