Debian Codegraph @

upstream/debian-trixie/man3/pthread_detach.3

raw
1 '\" t
2 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
3 .\" <[email protected]>
4 .\"
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\"
7 .TH pthread_detach 3 2024-05-02 "Linux man-pages 6.9.1"
8 .SH NAME
9 pthread_detach \- detach a thread
10 .SH LIBRARY
11 POSIX threads library
12 .RI ( libpthread ", " \-lpthread )
13 .SH SYNOPSIS
14 .nf
15 .B #include <pthread.h>
16 .P
17 .BI "int pthread_detach(pthread_t " thread );
18 .fi
19 .SH DESCRIPTION
20 The
21 .BR pthread_detach ()
22 function marks the thread identified by
23 .I thread
24 as detached.
25 When a detached thread terminates,
26 its resources are automatically released back to the system without
27 the need for another thread to join with the terminated thread.
28 .P
29 Attempting to detach an already detached thread results
30 in unspecified behavior.
31 .SH RETURN VALUE
32 On success,
33 .BR pthread_detach ()
34 returns 0;
35 on error, it returns an error number.
36 .SH ERRORS
37 .TP
38 .B EINVAL
39 .I thread
40 is not a joinable thread.
41 .TP
42 .B ESRCH
43 No thread with the ID
44 .I thread
45 could be found.
46 .SH ATTRIBUTES
47 For an explanation of the terms used in this section, see
48 .BR attributes (7).
49 .TS
50 allbox;
51 lbx lb lb
52 l l l.
53 Interface Attribute Value
54 T{
55 .na
56 .nh
57 .BR pthread_detach ()
58 T} Thread safety MT-Safe
59 .TE
60 .SH STANDARDS
61 POSIX.1-2008.
62 .SH HISTORY
63 POSIX.1-2001.
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 .P
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 .P
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 .P
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 .P
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)