1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH PTHREAD_DETACH 3 2021-03-22 "Linux" "Linux Programmer's Manual"
28 pthread_detach \- detach a thread
31 .B #include <pthread.h>
33 .BI "int pthread_detach(pthread_t " thread );
36 Compile and link with \fI\-pthread\fP.
40 function marks the thread identified by
43 When a detached thread terminates,
44 its resources are automatically released back to the system without
45 the need for another thread to join with the terminated thread.
47 Attempting to detach an already detached thread results
48 in unspecified behavior.
53 on error, it returns an error number.
58 is not a joinable thread.
65 For an explanation of the terms used in this section, see
73 Interface Attribute Value
76 T} Thread safety MT-Safe
82 POSIX.1-2001, POSIX.1-2008.
84 Once a thread has been detached, it can't be joined with
86 or be made joinable again.
88 A new thread can be created in a detached state using
89 .BR pthread_attr_setdetachstate (3)
90 to set the detached attribute of the
93 .BR pthread_create (3).
95 The detached attribute merely determines the behavior of the system
96 when the thread terminates;
97 it does not prevent the thread from being terminated
98 if the process terminates using
100 (or equivalently, if the main thread returns).
105 .BR pthread_detach ()
106 should be called for each thread that an application creates,
107 so that system resources for the thread can be released.
108 (But note that the resources of any threads for which one of these
109 actions has not been done will be freed when the process terminates.)
111 The following statement detaches the calling thread:
113 pthread_detach(pthread_self());
115 .BR pthread_attr_setdetachstate (3),
116 .BR pthread_cancel (3),
117 .BR pthread_create (3),
118 .BR pthread_exit (3),
119 .BR pthread_join (3),