1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH PTHREAD_SETCANCELSTATE 3 2021-03-22 "Linux man-pages (unreleased)" "Linux Programmer's Manual"
8 pthread_setcancelstate, pthread_setcanceltype \-
9 set cancelability state and type
12 .RI ( libpthread ", " \-lpthread )
15 .B #include <pthread.h>
17 .BI "int pthread_setcancelstate(int " state ", int *" oldstate );
18 .BI "int pthread_setcanceltype(int " type ", int *" oldtype );
22 .BR pthread_setcancelstate ()
23 sets the cancelability state of the calling thread to the value
26 The previous cancelability state of the thread is returned
27 in the buffer pointed to by
31 argument must have one of the following values:
33 .B PTHREAD_CANCEL_ENABLE
34 The thread is cancelable.
35 This is the default cancelability state in all new threads,
36 including the initial thread.
37 The thread's cancelability type determines when a cancelable thread
38 will respond to a cancelation request.
40 .B PTHREAD_CANCEL_DISABLE
41 The thread is not cancelable.
42 If a cancelation request is received,
43 it is blocked until cancelability is enabled.
46 .BR pthread_setcanceltype ()
47 sets the cancelability type of the calling thread to the value
50 The previous cancelability type of the thread is returned
51 in the buffer pointed to by
55 argument must have one of the following values:
57 .B PTHREAD_CANCEL_DEFERRED
58 A cancelation request is deferred until the thread next calls
59 a function that is a cancelation point (see
61 This is the default cancelability type in all new threads,
62 including the initial thread.
64 Even with deferred cancelation, a
65 cancelation point in an asynchronous signal handler may still
66 be acted upon and the effect is as if it was an asynchronous
69 .B PTHREAD_CANCEL_ASYNCHRONOUS
70 The thread can be canceled at any time.
72 it will be canceled immediately upon receiving a cancelation request,
73 but the system doesn't guarantee this.)
75 The set-and-get operation performed by each of these functions
76 is atomic with respect to other threads in the process
77 calling the same function.
79 On success, these functions return 0;
80 on error, they return a nonzero error number.
83 .BR pthread_setcancelstate ()
84 can fail with the following error:
91 .BR pthread_setcanceltype ()
92 can fail with the following error:
98 .\" Available since glibc 2.0
100 For an explanation of the terms used in this section, see
108 Interface Attribute Value
110 .BR pthread_setcancelstate (),
111 .BR pthread_setcanceltype ()
116 .BR pthread_setcancelstate (),
117 .BR pthread_setcanceltype ()
118 T} Async-cancel safety T{
127 POSIX.1-2001, POSIX.1-2008.
129 For details of what happens when a thread is canceled, see
130 .BR pthread_cancel (3).
132 Briefly disabling cancelability is useful
133 if a thread performs some critical action
134 that must not be interrupted by a cancelation request.
135 Beware of disabling cancelability for long periods,
136 or around operations that may block for long periods,
137 since that will render the thread unresponsive to cancelation requests.
138 .SS Asynchronous cancelability
139 Setting the cancelability type to
140 .B PTHREAD_CANCEL_ASYNCHRONOUS
142 Since the thread could be canceled at
144 time, it cannot safely reserve resources (e.g., allocating memory with
146 acquire mutexes, semaphores, or locks, and so on.
147 Reserving resources is unsafe because the application has no way of
148 knowing what the state of these resources is when the thread is canceled;
149 that is, did cancelation occur before the resources were reserved,
150 while they were reserved, or after they were released?
151 Furthermore, some internal data structures
152 (e.g., the linked list of free blocks managed by the
154 family of functions) may be left in an inconsistent state
155 if cancelation occurs in the middle of the function call.
156 Consequently, clean-up handlers cease to be useful.
158 Functions that can be safely asynchronously canceled are called
159 .IR "async-cancel-safe functions" .
160 POSIX.1-2001 and POSIX.1-2008 require only that
161 .BR pthread_cancel (3),
162 .BR pthread_setcancelstate (),
164 .BR pthread_setcanceltype ()
165 be async-cancel-safe.
166 In general, other library functions
167 can't be safely called from an asynchronously cancelable thread.
169 One of the few circumstances in which asynchronous cancelability is useful
170 is for cancelation of a thread that is in a pure compute-bound loop.
171 .SS Portability notes
172 The Linux threading implementations permit the
175 .BR pthread_setcancelstate ()
176 to be NULL, in which case the information about the previous
177 cancelability state is not returned to the caller.
178 Many other implementations also permit a NULL
181 .\" It looks like at least Solaris, FreeBSD and Tru64 support this.
182 but POSIX.1 does not specify this point,
183 so portable applications should always specify a non-NULL value in
185 A precisely analogous set of statements applies for the
188 .BR pthread_setcanceltype ().
191 .BR pthread_cancel (3).
193 .BR pthread_cancel (3),
194 .BR pthread_cleanup_push (3),
195 .BR pthread_testcancel (3),