]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/pthread_detach.3
All pages: Replace the 4th argument to .TH by "Linux man-pages (unreleased)"
[thirdparty/man-pages.git] / man3 / pthread_detach.3
1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH PTHREAD_DETACH 3 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
7 .SH NAME
8 pthread_detach \- detach a thread
9 .SH LIBRARY
10 POSIX threads library
11 .RI ( libpthread ", " \-lpthread )
12 .SH SYNOPSIS
13 .nf
14 .B #include <pthread.h>
15 .PP
16 .BI "int pthread_detach(pthread_t " thread );
17 .fi
18 .SH DESCRIPTION
19 The
20 .BR pthread_detach ()
21 function marks the thread identified by
22 .I thread
23 as detached.
24 When a detached thread terminates,
25 its resources are automatically released back to the system without
26 the need for another thread to join with the terminated thread.
27 .PP
28 Attempting to detach an already detached thread results
29 in unspecified behavior.
30 .SH RETURN VALUE
31 On success,
32 .BR pthread_detach ()
33 returns 0;
34 on error, it returns an error number.
35 .SH ERRORS
36 .TP
37 .B EINVAL
38 .I thread
39 is not a joinable thread.
40 .TP
41 .B ESRCH
42 No thread with the ID
43 .I thread
44 could be found.
45 .SH ATTRIBUTES
46 For an explanation of the terms used in this section, see
47 .BR attributes (7).
48 .ad l
49 .nh
50 .TS
51 allbox;
52 lbx lb lb
53 l l l.
54 Interface Attribute Value
55 T{
56 .BR pthread_detach ()
57 T} Thread safety MT-Safe
58 .TE
59 .hy
60 .ad
61 .sp 1
62 .SH STANDARDS
63 POSIX.1-2001, POSIX.1-2008.
64 .SH NOTES
65 Once a thread has been detached, it can't be joined with
66 .BR pthread_join (3)
67 or be made joinable again.
68 .PP
69 A new thread can be created in a detached state using
70 .BR pthread_attr_setdetachstate (3)
71 to set the detached attribute of the
72 .I attr
73 argument of
74 .BR pthread_create (3).
75 .PP
76 The detached attribute merely determines the behavior of the system
77 when the thread terminates;
78 it does not prevent the thread from being terminated
79 if the process terminates using
80 .BR exit (3)
81 (or equivalently, if the main thread returns).
82 .PP
83 Either
84 .BR pthread_join (3)
85 or
86 .BR pthread_detach ()
87 should be called for each thread that an application creates,
88 so that system resources for the thread can be released.
89 (But note that the resources of any threads for which one of these
90 actions has not been done will be freed when the process terminates.)
91 .SH EXAMPLES
92 The following statement detaches the calling thread:
93 .PP
94 .in +4n
95 .EX
96 pthread_detach(pthread_self());
97 .EE
98 .in
99 .SH SEE ALSO
100 .BR pthread_attr_setdetachstate (3),
101 .BR pthread_cancel (3),
102 .BR pthread_create (3),
103 .BR pthread_exit (3),
104 .BR pthread_join (3),
105 .BR pthreads (7)