1pthread_detach(3) Library Functions Manual pthread_detach(3)
2
3
4
6 pthread_detach - detach a thread
7
9 POSIX threads library (libpthread, -lpthread)
10
12 #include <pthread.h>
13
14 int pthread_detach(pthread_t thread);
15
17 The pthread_detach() function marks the thread identified by thread as
18 detached. When a detached thread terminates, its resources are auto‐
19 matically released back to the system without the need for another
20 thread to join with the terminated thread.
21
22 Attempting to detach an already detached thread results in unspecified
23 behavior.
24
26 On success, pthread_detach() returns 0; on error, it returns an error
27 number.
28
30 EINVAL thread is not a joinable thread.
31
32 ESRCH No thread with the ID thread could be found.
33
35 For an explanation of the terms used in this section, see at‐
36 tributes(7).
37
38 ┌────────────────────────────────────────────┬───────────────┬─────────┐
39 │Interface │ Attribute │ Value │
40 ├────────────────────────────────────────────┼───────────────┼─────────┤
41 │pthread_detach() │ Thread safety │ MT-Safe │
42 └────────────────────────────────────────────┴───────────────┴─────────┘
43
45 POSIX.1-2008.
46
48 POSIX.1-2001.
49
51 Once a thread has been detached, it can't be joined with
52 pthread_join(3) or be made joinable again.
53
54 A new thread can be created in a detached state using
55 pthread_attr_setdetachstate(3) to set the detached attribute of the
56 attr argument of pthread_create(3).
57
58 The detached attribute merely determines the behavior of the system
59 when the thread terminates; it does not prevent the thread from being
60 terminated if the process terminates using exit(3) (or equivalently, if
61 the main thread returns).
62
63 Either pthread_join(3) or pthread_detach() should be called for each
64 thread that an application creates, so that system resources for the
65 thread can be released. (But note that the resources of any threads
66 for which one of these actions has not been done will be freed when the
67 process terminates.)
68
70 The following statement detaches the calling thread:
71
72 pthread_detach(pthread_self());
73
75 pthread_attr_setdetachstate(3), pthread_cancel(3), pthread_create(3),
76 pthread_exit(3), pthread_join(3), pthreads(7)
77
78
79
80Linux man-pages 6.05 2023-07-20 pthread_detach(3)