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.
38 .BR pthread_setcancelstate ()
39 sets the cancelability state of the calling thread to the value
42 The previous cancelability state of the thread is returned
43 in the buffer pointed to by
47 argument must have one of the following values:
49 .B PTHREAD_CANCEL_ENABLE
50 The thread is cancelable.
51 This is the default cancelability state in all new threads,
52 including the initial thread.
53 The thread's cancelability type determines when a cancelable thread
54 will respond to a cancellation request.
56 .B PTHREAD_CANCEL_DISABLE
57 The thread is not cancelable.
58 If a cancellation request is received,
59 it is blocked until cancelability is enabled.
62 .BR pthread_setcanceltype ()
63 sets the cancelability type of the calling thread to the value
66 The previous cancelability type of the thread is returned
67 in the buffer pointed to by
71 argument must have one of the following values:
73 .B PTHREAD_CANCEL_DEFERRED
74 A cancellation request is deferred until the thread next calls
75 a function that is a cancellation point (see
77 This is the default cancelability type in all new threads,
78 including the initial thread.
80 .B PTHREAD_CANCEL_ASYNCHRONOUS
81 The thread can be canceled at any time.
83 it will be canceled immediately upon receiving a cancellation request,
84 but the system doesn't guarantee this.)
86 The set-and-get operation performed by each of these functions
87 is atomic with respect to other threads in the process
88 calling the same function.
90 On success, these functions return 0;
91 on error, they return a nonzero error number.
94 .BR pthread_setcancelstate ()
95 can fail with the following error:
102 .BR pthread_setcanceltype ()
103 can fail with the following error:
109 .\" Available since glibc 2.0
113 For details of what happens when a thread is canceled, see
114 .BR pthread_cancel (3).
116 Briefly disabling cancelability is useful
117 if a thread performs some critical action
118 that must not be interrupted by a cancellation request.
119 Beware of disabling cancelability for long periods,
120 or around operations that may block for long periods,
121 since that will render the thread unresponsive to cancellation requests.
123 Setting the cancelability type to
124 .B PTHREAD_CANCEL_ASYNCHRONOUS
126 Since the thread could be canceled at
128 time, it cannot safely reserve resources (e.g., allocating memory with
130 acquire mutexes, semaphores, or locks, and so on.
131 Reserving resources is unsafe because the application has no way of
132 knowing what the state of these resources is when the thread is canceled;
133 that is, did cancellation occur before the resources were reserved,
134 while they were reserved, or after they were released?
135 Furthermore, some internal data structures
136 (e.g., the linked list of free blocks managed by the
138 family of functions) may be left in an inconsistent state
139 if cancellation occurs in the middle of the function call.
140 Consequently, clean-up handlers cease to be useful.
141 Functions that can be safely asynchronously canceled are called
142 .IR "async-cancel-safe functions" .
143 POSIX.1-2001 only requires that
144 .BR pthread_cancel (3),
145 .BR pthread_setcancelstate (),
147 .BR pthread_setcanceltype ()
148 be async-cancel-safe.
149 In general, other library functions
150 can't be safely called from an asynchronously cancelable thread.
151 One of the few circumstances in which asynchronous cancelability is useful
152 is for cancellation of a thread that is in a pure compute-bound loop.
154 The Linux threading implementations permit the
157 .BR pthread_setcancelstate ()
158 to be NULL, in which case the information about the previous
159 cancelability state is not returned to the caller.
160 Many other implementations also permit a NULL
163 .\" It looks like at least Solaris, FreeBSD and Tru64 support this.
164 but POSIX.1-2001 does not specify this point,
165 so portable applications should always specify a non-NULL value in
167 A precisely analogous set of statements applies for the
170 .BR pthread_setcanceltype ().
173 .BR pthread_cancel (3).
175 .BR pthread_cancel (3),
176 .BR pthread_cleanup_push (3),
177 .BR pthread_testcancel (3),