Debian Codegraph @

upstream/debian-unstable/man3/pthread_setcancelstate.3

raw
1 '\" t
2 .\" Copyright, the authors of the Linux man-pages project
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH pthread_setcancelstate 3 2025-05-17 "Linux man-pages 6.18"
7 .SH NAME
8 pthread_setcancelstate, pthread_setcanceltype \-
9 set cancelability state and type
10 .SH LIBRARY
11 POSIX threads library
12 .RI ( libpthread ,\~ \-lpthread )
13 .SH SYNOPSIS
14 .nf
15 .B #include <pthread.h>
16 .P
17 .BI "int pthread_setcancelstate(int " state ", int *" oldstate );
18 .BI "int pthread_setcanceltype(int " type ", int *" oldtype );
19 .fi
20 .SH DESCRIPTION
21 The
22 .BR pthread_setcancelstate ()
23 sets the cancelability state of the calling thread to the value
24 given in
25 .IR state .
26 The previous cancelability state of the thread is returned
27 in the buffer pointed to by
28 .IR oldstate .
29 The
30 .I state
31 argument must have one of the following values:
32 .TP
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.
39 .TP
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.
44 .P
45 The
46 .BR pthread_setcanceltype ()
47 sets the cancelability type of the calling thread to the value
48 given in
49 .IR type .
50 The previous cancelability type of the thread is returned
51 in the buffer pointed to by
52 .IR oldtype .
53 The
54 .I type
55 argument must have one of the following values:
56 .TP
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
60 .BR pthreads (7)).
61 This is the default cancelability type in all new threads,
62 including the initial thread.
63 .IP
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
67 cancelation.
68 .TP
69 .B PTHREAD_CANCEL_ASYNCHRONOUS
70 The thread can be canceled at any time.
71 (Typically,
72 it will be canceled immediately upon receiving a cancelation request,
73 but the system doesn't guarantee this.)
74 .P
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.
78 .SH RETURN VALUE
79 On success, these functions return 0;
80 on error, they return a nonzero error number.
81 .SH ERRORS
82 The
83 .BR pthread_setcancelstate ()
84 can fail with the following error:
85 .TP
86 .B EINVAL
87 Invalid value for
88 .IR state .
89 .P
90 The
91 .BR pthread_setcanceltype ()
92 can fail with the following error:
93 .TP
94 .B EINVAL
95 Invalid value for
96 .IR type .
97 .SH ATTRIBUTES
98 For an explanation of the terms used in this section, see
99 .BR attributes (7).
100 .TS
101 allbox;
102 lbx lb lb
103 l l l.
104 Interface Attribute Value
105 T{
106 .na
107 .nh
108 .BR pthread_setcancelstate (),
109 .BR pthread_setcanceltype ()
110 T} Thread safety T{
111 .na
112 .nh
113 MT-Safe
114 T}
115 T{
116 .na
117 .nh
118 .BR pthread_setcancelstate (),
119 .BR pthread_setcanceltype ()
120 T} Async-cancel safety T{
121 .na
122 .nh
123 AC-Safe
124 T}
125 .TE
126 .SH STANDARDS
127 POSIX.1-2008.
128 .SH HISTORY
129 glibc 2.0
130 POSIX.1-2001.
131 .SH NOTES
132 For details of what happens when a thread is canceled, see
133 .BR \%pthread_cancel (3).
134 .P
135 Briefly disabling cancelability is useful
136 if a thread performs some critical action
137 that must not be interrupted by a cancelation request.
138 Beware of disabling cancelability for long periods,
139 or around operations that may block for long periods,
140 since that will render the thread unresponsive to cancelation requests.
141 .SS Asynchronous cancelability
142 Setting the cancelability type to
143 .B PTHREAD_CANCEL_ASYNCHRONOUS
144 is rarely useful.
145 Since the thread could be canceled at
146 .I any
147 time, it cannot safely reserve resources (e.g., allocating memory with
148 .BR malloc (3)),
149 acquire mutexes, semaphores, or locks, and so on.
150 Reserving resources is unsafe because the application has no way of
151 knowing what the state of these resources is when the thread is canceled;
152 that is, did cancelation occur before the resources were reserved,
153 while they were reserved, or after they were released?
154 Furthermore, some internal data structures
155 (e.g., the linked list of free blocks managed by the
156 .BR malloc (3)
157 family of functions) may be left in an inconsistent state
158 if cancelation occurs in the middle of the function call.
159 Consequently, clean-up handlers cease to be useful.
160 .P
161 Functions that can be safely asynchronously canceled are called
162 .IR "async-cancel-safe functions" .
163 POSIX.1-2001 and POSIX.1-2008 require only that
164 .BR pthread_cancel (3),
165 .BR pthread_setcancelstate (),
166 and
167 .BR pthread_setcanceltype ()
168 be async-cancel-safe.
169 In general, other library functions
170 can't be safely called from an asynchronously cancelable thread.
171 .P
172 One of the few circumstances in which asynchronous cancelability is useful
173 is for cancelation of a thread that is in a pure compute-bound loop.
174 .SS Portability notes
175 The Linux threading implementations permit the
176 .I oldstate
177 argument of
178 .BR pthread_setcancelstate ()
179 to be NULL, in which case the information about the previous
180 cancelability state is not returned to the caller.
181 Many other implementations also permit a NULL
182 .I oldstat
183 argument,
184 .\" It looks like at least Solaris, FreeBSD and Tru64 support this.
185 but POSIX.1 does not specify this point,
186 so portable applications should always specify a non-NULL value in
187 .IR oldstate .
188 A precisely analogous set of statements applies for the
189 .I oldtype
190 argument of
191 .BR pthread_setcanceltype ().
192 .SH EXAMPLES
193 See
194 .BR pthread_cancel (3).
195 .SH SEE ALSO
196 .BR pthread_cancel (3),
197 .BR pthread_cleanup_push (3),
198 .BR pthread_testcancel (3),
199 .BR pthreads (7)