OSDN Git Service

be6256d7805de87b2d677b2bb761b936bd414cfc
[linuxjm/LDP_man-pages.git] / original / man3 / pthread_detach.3
1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\"     <mtk.manpages@gmail.com>
3 .\"
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\"
24 .TH PTHREAD_DETACH 3 2008-11-27 "Linux" "Linux Programmer's Manual"
25 .SH NAME
26 pthread_detach \- detach a thread
27 .SH SYNOPSIS
28 .nf
29 .B #include <pthread.h>
30
31 .BI "int pthread_detach(pthread_t " thread );
32 .fi
33 .sp
34 Compile and link with \fI\-pthread\fP.
35 .SH DESCRIPTION
36 The
37 .BR pthread_detach ()
38 function marks the thread identified by
39 .IR thread
40 as detached.
41 When a detached thread terminates,
42 its resources are automatically released back to the system without
43 the need for another thread to join with the terminated thread.
44
45 Attempting to detach an already detached thread results
46 in unspecified behavior.
47 .SH RETURN VALUE
48 On success,
49 .BR pthread_detach ()
50 returns 0;
51 on error, it returns an error number.
52 .SH ERRORS
53 .TP
54 .B EINVAL
55 .I thread
56 is not a joinable thread.
57 .TP
58 .B ESRCH
59 No thread with the ID
60 .I thread
61 could be found.
62 .SH CONFORMING TO
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
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
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
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 all threads are freed when the
90 process terminates.)
91 .SH EXAMPLE
92 The following statement detaches the calling thread:
93
94     pthread_detach(pthread_self());
95 .SH SEE ALSO
96 .BR pthread_attr_setdetachstate (3),
97 .BR pthread_cancel (3),
98 .BR pthread_create (3),
99 .BR pthread_exit (3),
100 .BR pthread_join (3),
101 .BR pthreads (7)