OSDN Git Service

(split) LDP: Update original to LDP v3.38.
[linuxjm/LDP_man-pages.git] / original / man3 / pthread_setcancelstate.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_SETCANCELSTATE 3 2008-11-24 "Linux" "Linux Programmer's Manual"
25 .SH NAME
26 pthread_setcancelstate, pthread_setcanceltype \-
27 set cancelability state and type
28 .SH SYNOPSIS
29 .nf
30 .B #include <pthread.h>
31
32 .BI "int pthread_setcancelstate(int " state ", int *" oldstate );
33 .BI "int pthread_setcanceltype(int " type ", int *" oldtype );
34 .sp
35 Compile and link with \fI\-pthread\fP.
36 .fi
37 .SH DESCRIPTION
38 The
39 .BR pthread_setcancelstate ()
40 sets the cancelability state of the calling thread to the value
41 given in
42 .IR state .
43 The previous cancelability state of the thread is returned
44 in the buffer pointed to by
45 .IR oldstate .
46 The
47 .I state
48 argument must have one of the following values:
49 .TP
50 .B PTHREAD_CANCEL_ENABLE
51 The thread is cancelable.
52 This is the default cancelability state in all new threads,
53 including the initial thread.
54 The thread's cancelability type determines when a cancelable thread
55 will respond to a cancellation request.
56 .TP
57 .B PTHREAD_CANCEL_DISABLE
58 The thread is not cancelable.
59 If a cancellation request is received,
60 it is blocked until cancelability is enabled.
61 .PP
62 The
63 .BR pthread_setcanceltype ()
64 sets the cancelability type of the calling thread to the value
65 given in
66 .IR type .
67 The previous cancelability type of the thread is returned
68 in the buffer pointed to by
69 .IR oldtype .
70 The
71 .I type
72 argument must have one of the following values:
73 .TP
74 .B PTHREAD_CANCEL_DEFERRED
75 A cancellation request is deferred until the thread next calls
76 a function that is a cancellation point (see
77 .BR pthreads (7)).
78 This is the default cancelability type in all new threads,
79 including the initial thread.
80 .TP
81 .B PTHREAD_CANCEL_ASYNCHRONOUS
82 The thread can be canceled at any time.
83 (Typically,
84 it will be canceled immediately upon receiving a cancellation request,
85 but the system doesn't guarantee this.)
86 .PP
87 The set-and-get operation performed by each of these functions
88 is atomic with respect to other threads in the process
89 calling the same function.
90 .SH RETURN VALUE
91 On success, these functions return 0;
92 on error, they return a nonzero error number.
93 .SH ERRORS
94 The
95 .BR pthread_setcancelstate ()
96 can fail with the following error:
97 .TP
98 .B EINVAL
99 Invalid value for
100 .IR state .
101 .PP
102 The
103 .BR pthread_setcanceltype ()
104 can fail with the following error:
105 .TP
106 .B EINVAL
107 Invalid value for
108 .IR type .
109 .\" .SH VERSIONS
110 .\" Available since glibc 2.0
111 .SH CONFORMING TO
112 POSIX.1-2001.
113 .SH NOTES
114 For details of what happens when a thread is canceled, see
115 .BR pthread_cancel (3).
116
117 Briefly disabling cancelability is useful
118 if a thread performs some critical action
119 that must not be interrupted by a cancellation request.
120 Beware of disabling cancelability for long periods,
121 or around operations that may block for long periods,
122 since that will render the thread unresponsive to cancellation requests.
123
124 Setting the cancelability type to
125 .B PTHREAD_CANCEL_ASYNCHRONOUS
126 is rarely useful.
127 Since the thread could be canceled at
128 .I any
129 time, it cannot safely reserve resources (e.g., allocating memory with
130 .BR malloc (3)),
131 acquire mutexes, semaphores, or locks, and so on.
132 Reserving resources is unsafe because the application has no way of
133 knowing what the state of these resources is when the thread is canceled;
134 that is, did cancellation occur before the resources were reserved,
135 while they were reserved, or after they were released?
136 Furthermore, some internal data structures
137 (e.g., the linked list of free blocks managed by the
138 .BR malloc (3)
139 family of functions) may be left in an inconsistent state
140 if cancellation occurs in the middle of the function call.
141 Consequently, clean-up handlers cease to be useful.
142 Functions that can be safely asynchronously canceled are called
143 .IR "async-cancel-safe functions" .
144 POSIX.1-2001 only requires that
145 .BR pthread_cancel (3),
146 .BR pthread_setcancelstate (),
147 and
148 .BR pthread_setcanceltype ()
149 be async-cancel-safe.
150 In general, other library functions
151 can't be safely called from an asynchronously cancelable thread.
152 One of the few circumstances in which asynchronous cancelability is useful
153 is for cancellation of a thread that is in a pure compute-bound loop.
154
155 The Linux threading implementations permit the
156 .I oldstate
157 argument of
158 .BR pthread_setcancelstate ()
159 to be NULL, in which case the information about the previous
160 cancelability state is not returned to the caller.
161 Many other implementations also permit a NULL
162 .I oldstat
163 argument,
164 .\" It looks like at least Solaris, FreeBSD and Tru64 support this.
165 but POSIX.1-2001 does not specify this point,
166 so portable applications should always specify a non-NULL value in
167 .IR oldstate .
168 A precisely analogous set of statements applies for the
169 .I oldtype
170 argument of
171 .BR pthread_setcanceltype ().
172 .SH EXAMPLE
173 See
174 .BR pthread_cancel (3).
175 .SH SEE ALSO
176 .BR pthread_cancel (3),
177 .BR pthread_cleanup_push (3),
178 .BR pthread_testcancel (3),
179 .BR pthreads (7)