1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
.\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
.\" <mtk.manpages@gmail.com>
.\"
.\" %%%LICENSE_START(VERBATIM)
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein. The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\" %%%LICENSE_END
.\"
.TH PTHREAD_SETCANCELSTATE 3 2017-09-15 "Linux" "Linux Programmer's Manual"
.SH NAME
pthread_setcancelstate, pthread_setcanceltype \-
set cancelability state and type
.SH SYNOPSIS
.nf
.B #include <pthread.h>
.PP
.BI "int pthread_setcancelstate(int " state ", int *" oldstate );
.BI "int pthread_setcanceltype(int " type ", int *" oldtype );
.PP
Compile and link with \fI\-pthread\fP.
.fi
.SH DESCRIPTION
The
.BR pthread_setcancelstate ()
sets the cancelability state of the calling thread to the value
given in
.IR state .
The previous cancelability state of the thread is returned
in the buffer pointed to by
.IR oldstate .
The
.I state
argument must have one of the following values:
.TP
.B PTHREAD_CANCEL_ENABLE
The thread is cancelable.
This is the default cancelability state in all new threads,
including the initial thread.
The thread's cancelability type determines when a cancelable thread
will respond to a cancellation request.
.TP
.B PTHREAD_CANCEL_DISABLE
The thread is not cancelable.
If a cancellation request is received,
it is blocked until cancelability is enabled.
.PP
The
.BR pthread_setcanceltype ()
sets the cancelability type of the calling thread to the value
given in
.IR type .
The previous cancelability type of the thread is returned
in the buffer pointed to by
.IR oldtype .
The
.I type
argument must have one of the following values:
.TP
.B PTHREAD_CANCEL_DEFERRED
A cancellation request is deferred until the thread next calls
a function that is a cancellation point (see
.BR pthreads (7)).
This is the default cancelability type in all new threads,
including the initial thread.
.TP
.B PTHREAD_CANCEL_ASYNCHRONOUS
The thread can be canceled at any time.
(Typically,
it will be canceled immediately upon receiving a cancellation request,
but the system doesn't guarantee this.)
.PP
The set-and-get operation performed by each of these functions
is atomic with respect to other threads in the process
calling the same function.
.SH RETURN VALUE
On success, these functions return 0;
on error, they return a nonzero error number.
.SH ERRORS
The
.BR pthread_setcancelstate ()
can fail with the following error:
.TP
.B EINVAL
Invalid value for
.IR state .
.PP
The
.BR pthread_setcanceltype ()
can fail with the following error:
.TP
.B EINVAL
Invalid value for
.IR type .
.\" .SH VERSIONS
.\" Available since glibc 2.0
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.ad l
.TS
allbox;
lb lb lb
lw25 l l.
Interface Attribute Value
T{
.BR pthread_setcancelstate (),
.BR pthread_setcanceltype ()
T} Thread safety T{
MT-Safe
T}
T{
.BR pthread_setcancelstate (),
.BR pthread_setcanceltype ()
T} Async-cancel-safety T{
AC-Safe
T}
.TE
.ad
.hy
.SH CONFORMING TO
POSIX.1-2001, POSIX.1-2008.
.SH NOTES
For details of what happens when a thread is canceled, see
.BR pthread_cancel (3).
.PP
Briefly disabling cancelability is useful
if a thread performs some critical action
that must not be interrupted by a cancellation request.
Beware of disabling cancelability for long periods,
or around operations that may block for long periods,
since that will render the thread unresponsive to cancellation requests.
.SS Asynchronous cancelability
Setting the cancelability type to
.B PTHREAD_CANCEL_ASYNCHRONOUS
is rarely useful.
Since the thread could be canceled at
.I any
time, it cannot safely reserve resources (e.g., allocating memory with
.BR malloc (3)),
acquire mutexes, semaphores, or locks, and so on.
Reserving resources is unsafe because the application has no way of
knowing what the state of these resources is when the thread is canceled;
that is, did cancellation occur before the resources were reserved,
while they were reserved, or after they were released?
Furthermore, some internal data structures
(e.g., the linked list of free blocks managed by the
.BR malloc (3)
family of functions) may be left in an inconsistent state
if cancellation occurs in the middle of the function call.
Consequently, clean-up handlers cease to be useful.
.PP
Functions that can be safely asynchronously canceled are called
.IR "async-cancel-safe functions" .
POSIX.1-2001 and POSIX.1-2008 require only that
.BR pthread_cancel (3),
.BR pthread_setcancelstate (),
and
.BR pthread_setcanceltype ()
be async-cancel-safe.
In general, other library functions
can't be safely called from an asynchronously cancelable thread.
.PP
One of the few circumstances in which asynchronous cancelability is useful
is for cancellation of a thread that is in a pure compute-bound loop.
.SS Portability notes
The Linux threading implementations permit the
.I oldstate
argument of
.BR pthread_setcancelstate ()
to be NULL, in which case the information about the previous
cancelability state is not returned to the caller.
Many other implementations also permit a NULL
.I oldstat
argument,
.\" It looks like at least Solaris, FreeBSD and Tru64 support this.
but POSIX.1 does not specify this point,
so portable applications should always specify a non-NULL value in
.IR oldstate .
A precisely analogous set of statements applies for the
.I oldtype
argument of
.BR pthread_setcanceltype ().
.SH EXAMPLE
See
.BR pthread_cancel (3).
.SH SEE ALSO
.BR pthread_cancel (3),
.BR pthread_cleanup_push (3),
.BR pthread_testcancel (3),
.BR pthreads (7)
.SH COLOPHON
This page is part of release 4.16 of the Linux
.I man-pages
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
\%https://www.kernel.org/doc/man\-pages/.
|