1PTHREAD_DETACH(3)          Linux Programmer's Manual         PTHREAD_DETACH(3)
2
3
4

NAME

6       pthread_detach - detach a thread
7

SYNOPSIS

9       #include <pthread.h>
10
11       int pthread_detach(pthread_t thread);
12
13       Compile and link with -pthread.
14

DESCRIPTION

16       The  pthread_detach() function marks the thread identified by thread as
17       detached.  When a detached thread terminates, its resources  are  auto‐
18       matically  released  back  to  the  system without the need for another
19       thread to join with the terminated thread.
20
21       Attempting to detach an already detached thread results in  unspecified
22       behavior.
23

RETURN VALUE

25       On  success,  pthread_detach() returns 0; on error, it returns an error
26       number.
27

ERRORS

29       EINVAL thread is not a joinable thread.
30
31       ESRCH  No thread with the ID thread could be found.
32

CONFORMING TO

34       POSIX.1-2001.
35

NOTES

37       Once  a  thread  has  been  detached,   it   can't   be   joined   with
38       pthread_join(3) or be made joinable again.
39
40       A new thread can be created in a detached state using pthread_attr_set‐
41       detachstate(3) to set the detached attribute of the  attr  argument  of
42       pthread_create(3).
43
44       The  detached  attribute  merely  determines the behavior of the system
45       when the thread terminates; it does not prevent the thread  from  being
46       terminated if the process terminates using exit(3) (or equivalently, if
47       the main thread returns).
48
49       Either pthread_join(3) or pthread_detach() should be  called  for  each
50       thread  that  an  application creates, so that system resources for the
51       thread can be released.  (But note that the resources  of  all  threads
52       are freed when the process terminates.)
53

EXAMPLE

55       The following statement detaches the calling thread:
56
57           pthread_detach(pthread_self());
58

SEE ALSO

60       pthread_attr_setdetachstate(3),  pthread_cancel(3),  pthread_create(3),
61       pthread_exit(3), pthread_join(3), pthreads(7)
62

COLOPHON

64       This page is part of release 3.53 of the Linux  man-pages  project.   A
65       description  of  the project, and information about reporting bugs, can
66       be found at http://www.kernel.org/doc/man-pages/.
67
68
69
70Linux                             2008-11-27                 PTHREAD_DETACH(3)
Impressum