1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .TH PTHREAD_SETCANCELSTATE 3 2008-11-24 "Linux" "Linux Programmer's Manual"
26 pthread_setcancelstate, pthread_setcanceltype \-
27 set cancelability state and type
30 .B #include <pthread.h>
32 .BI "int pthread_setcancelstate(int " state ", int *" oldstate );
33 .BI "int pthread_setcanceltype(int " type ", int *" oldtype );
35 Compile and link with \fI\-pthread\fP.
39 .BR pthread_setcancelstate ()
40 sets the cancelability state of the calling thread to the value
43 The previous cancelability state of the thread is returned
44 in the buffer pointed to by
48 argument must have one of the following values:
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.
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.
63 .BR pthread_setcanceltype ()
64 sets the cancelability type of the calling thread to the value
67 The previous cancelability type of the thread is returned
68 in the buffer pointed to by
72 argument must have one of the following values:
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
78 This is the default cancelability type in all new threads,
79 including the initial thread.
81 .B PTHREAD_CANCEL_ASYNCHRONOUS
82 The thread can be canceled at any time.
84 it will be canceled immediately upon receiving a cancellation request,
85 but the system doesn't guarantee this.)
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.
91 On success, these functions return 0;
92 on error, they return a nonzero error number.
95 .BR pthread_setcancelstate ()
96 can fail with the following error:
103 .BR pthread_setcanceltype ()
104 can fail with the following error:
110 .\" Available since glibc 2.0
114 For details of what happens when a thread is canceled, see
115 .BR pthread_cancel (3).
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.
124 Setting the cancelability type to
125 .B PTHREAD_CANCEL_ASYNCHRONOUS
127 Since the thread could be canceled at
129 time, it cannot safely reserve resources (e.g., allocating memory with
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
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 (),
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.
155 The Linux threading implementations permit the
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
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
168 A precisely analogous set of statements applies for the
171 .BR pthread_setcanceltype ().
174 .BR pthread_cancel (3).
176 .BR pthread_cancel (3),
177 .BR pthread_cleanup_push (3),
178 .BR pthread_testcancel (3),