]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/pthread_setcancelstate.3
All pages: Remove the 5th argument to .TH
[thirdparty/man-pages.git] / man3 / pthread_setcancelstate.3
1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
3 .\"
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\"
6 .TH PTHREAD_SETCANCELSTATE 3 2021-03-22 "Linux man-pages (unreleased)"
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 .PP
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 .PP
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 .PP
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 .PP
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 VERSIONS
98 .\" Available since glibc 2.0
99 .SH ATTRIBUTES
100 For an explanation of the terms used in this section, see
101 .BR attributes (7).
102 .ad l
103 .nh
104 .TS
105 allbox;
106 lbx lb lb
107 l l l.
108 Interface Attribute Value
109 T{
110 .BR pthread_setcancelstate (),
111 .BR pthread_setcanceltype ()
112 T} Thread safety T{
113 MT-Safe
114 T}
115 T{
116 .BR pthread_setcancelstate (),
117 .BR pthread_setcanceltype ()
118 T} Async-cancel safety T{
119 AC-Safe
120 T}
121 .TE
122 .hy
123 .ad
124 .sp 1
125 .hy
126 .SH STANDARDS
127 POSIX.1-2001, POSIX.1-2008.
128 .SH NOTES
129 For details of what happens when a thread is canceled, see
130 .BR pthread_cancel (3).
131 .PP
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
141 is rarely useful.
142 Since the thread could be canceled at
143 .I any
144 time, it cannot safely reserve resources (e.g., allocating memory with
145 .BR malloc (3)),
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
153 .BR malloc (3)
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.
157 .PP
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 (),
163 and
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.
168 .PP
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
173 .I oldstate
174 argument of
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
179 .I oldstat
180 argument,
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
184 .IR oldstate .
185 A precisely analogous set of statements applies for the
186 .I oldtype
187 argument of
188 .BR pthread_setcanceltype ().
189 .SH EXAMPLES
190 See
191 .BR pthread_cancel (3).
192 .SH SEE ALSO
193 .BR pthread_cancel (3),
194 .BR pthread_cleanup_push (3),
195 .BR pthread_testcancel (3),
196 .BR pthreads (7)