]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/pthread_setcancelstate.3
All pages: Remove the 5th argument to .TH
[thirdparty/man-pages.git] / man3 / pthread_setcancelstate.3
CommitLineData
ccb42cb8
MK
1.\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2.\" <mtk.manpages@gmail.com>
3.\"
5fbde956 4.\" SPDX-License-Identifier: Linux-man-pages-copyleft
ccb42cb8 5.\"
45186a5d 6.TH PTHREAD_SETCANCELSTATE 3 2021-03-22 "Linux man-pages (unreleased)"
ccb42cb8
MK
7.SH NAME
8pthread_setcancelstate, pthread_setcanceltype \-
9set cancelability state and type
660c7612
AC
10.SH LIBRARY
11POSIX threads library
8fc3b2cf 12.RI ( libpthread ", " \-lpthread )
ccb42cb8
MK
13.SH SYNOPSIS
14.nf
15.B #include <pthread.h>
dbfe9c70 16.PP
ccb42cb8
MK
17.BI "int pthread_setcancelstate(int " state ", int *" oldstate );
18.BI "int pthread_setcanceltype(int " type ", int *" oldtype );
6030f2d8 19.fi
ccb42cb8
MK
20.SH DESCRIPTION
21The
22.BR pthread_setcancelstate ()
23sets the cancelability state of the calling thread to the value
24given in
25.IR state .
26The previous cancelability state of the thread is returned
27in the buffer pointed to by
28.IR oldstate .
29The
30.I state
31argument must have one of the following values:
32.TP
33.B PTHREAD_CANCEL_ENABLE
34The thread is cancelable.
35This is the default cancelability state in all new threads,
36including the initial thread.
37The thread's cancelability type determines when a cancelable thread
1f08fc80 38will respond to a cancelation request.
ccb42cb8
MK
39.TP
40.B PTHREAD_CANCEL_DISABLE
41The thread is not cancelable.
1f08fc80 42If a cancelation request is received,
ccb42cb8
MK
43it is blocked until cancelability is enabled.
44.PP
45The
46.BR pthread_setcanceltype ()
47sets the cancelability type of the calling thread to the value
48given in
49.IR type .
50The previous cancelability type of the thread is returned
51in the buffer pointed to by
52.IR oldtype .
53The
54.I type
55argument must have one of the following values:
56.TP
57.B PTHREAD_CANCEL_DEFERRED
1f08fc80
SN
58A cancelation request is deferred until the thread next calls
59a function that is a cancelation point (see
ccb42cb8
MK
60.BR pthreads (7)).
61This is the default cancelability type in all new threads,
50639a2a 62including the initial thread.
0b6cf5d2 63.IP
1f08fc80
SN
64Even with deferred cancelation, a
65cancelation point in an asynchronous signal handler may still
0b6cf5d2 66be acted upon and the effect is as if it was an asynchronous
1f08fc80 67cancelation.
ccb42cb8
MK
68.TP
69.B PTHREAD_CANCEL_ASYNCHRONOUS
70The thread can be canceled at any time.
71(Typically,
1f08fc80 72it will be canceled immediately upon receiving a cancelation request,
ccb42cb8
MK
73but the system doesn't guarantee this.)
74.PP
75The set-and-get operation performed by each of these functions
76is atomic with respect to other threads in the process
77calling the same function.
78.SH RETURN VALUE
79On success, these functions return 0;
c7094399 80on error, they return a nonzero error number.
ccb42cb8
MK
81.SH ERRORS
82The
83.BR pthread_setcancelstate ()
84can fail with the following error:
85.TP
86.B EINVAL
87Invalid value for
88.IR state .
89.PP
90The
91.BR pthread_setcanceltype ()
92can fail with the following error:
93.TP
94.B EINVAL
95Invalid value for
96.IR type .
97.\" .SH VERSIONS
98.\" Available since glibc 2.0
50b60123 99.SH ATTRIBUTES
dfa2186c
MK
100For an explanation of the terms used in this section, see
101.BR attributes (7).
102.ad l
c466875e 103.nh
dfa2186c
MK
104.TS
105allbox;
c466875e
MK
106lbx lb lb
107l l l.
dfa2186c
MK
108Interface Attribute Value
109T{
110.BR pthread_setcancelstate (),
50b60123 111.BR pthread_setcanceltype ()
dfa2186c
MK
112T} Thread safety T{
113MT-Safe
114T}
f385a71d
MK
115T{
116.BR pthread_setcancelstate (),
117.BR pthread_setcanceltype ()
b32feea5 118T} Async-cancel safety T{
f385a71d
MK
119AC-Safe
120T}
dfa2186c 121.TE
c466875e 122.hy
dfa2186c 123.ad
c466875e 124.sp 1
dfa2186c 125.hy
3113c7f3 126.SH STANDARDS
1d426e09 127POSIX.1-2001, POSIX.1-2008.
ccb42cb8
MK
128.SH NOTES
129For details of what happens when a thread is canceled, see
130.BR pthread_cancel (3).
847e0d88 131.PP
ccb42cb8
MK
132Briefly disabling cancelability is useful
133if a thread performs some critical action
1f08fc80 134that must not be interrupted by a cancelation request.
ccb42cb8
MK
135Beware of disabling cancelability for long periods,
136or around operations that may block for long periods,
1f08fc80 137since that will render the thread unresponsive to cancelation requests.
fad1a267 138.SS Asynchronous cancelability
ccb42cb8
MK
139Setting the cancelability type to
140.B PTHREAD_CANCEL_ASYNCHRONOUS
141is rarely useful.
142Since the thread could be canceled at
143.I any
ee1534cb
MK
144time, it cannot safely reserve resources (e.g., allocating memory with
145.BR malloc (3)),
146acquire mutexes, semaphores, or locks, and so on.
f5410853 147Reserving resources is unsafe because the application has no way of
ee1534cb 148knowing what the state of these resources is when the thread is canceled;
1f08fc80 149that is, did cancelation occur before the resources were reserved,
ee1534cb 150while they were reserved, or after they were released?
a86f0a4b
MK
151Furthermore, some internal data structures
152(e.g., the linked list of free blocks managed by the
153.BR malloc (3)
154family of functions) may be left in an inconsistent state
1f08fc80 155if cancelation occurs in the middle of the function call.
ee1534cb 156Consequently, clean-up handlers cease to be useful.
847e0d88 157.PP
ee1534cb 158Functions that can be safely asynchronously canceled are called
a113945f 159.IR "async-cancel-safe functions" .
1d426e09 160POSIX.1-2001 and POSIX.1-2008 require only that
ccb42cb8
MK
161.BR pthread_cancel (3),
162.BR pthread_setcancelstate (),
163and
164.BR pthread_setcanceltype ()
ee1534cb
MK
165be async-cancel-safe.
166In general, other library functions
167can't be safely called from an asynchronously cancelable thread.
847e0d88 168.PP
ccb42cb8 169One of the few circumstances in which asynchronous cancelability is useful
1f08fc80 170is for cancelation of a thread that is in a pure compute-bound loop.
fad1a267 171.SS Portability notes
ccb42cb8
MK
172The Linux threading implementations permit the
173.I oldstate
174argument of
175.BR pthread_setcancelstate ()
176to be NULL, in which case the information about the previous
177cancelability state is not returned to the caller.
178Many other implementations also permit a NULL
179.I oldstat
180argument,
181.\" It looks like at least Solaris, FreeBSD and Tru64 support this.
1d426e09 182but POSIX.1 does not specify this point,
ccb42cb8
MK
183so portable applications should always specify a non-NULL value in
184.IR oldstate .
185A precisely analogous set of statements applies for the
186.I oldtype
187argument of
188.BR pthread_setcanceltype ().
a14af333 189.SH EXAMPLES
ccb42cb8
MK
190See
191.BR pthread_cancel (3).
192.SH SEE ALSO
ccb42cb8 193.BR pthread_cancel (3),
3e5c319e 194.BR pthread_cleanup_push (3),
ccb42cb8
MK
195.BR pthread_testcancel (3),
196.BR pthreads (7)