man-pages/man3/pthread_setcancelstate.3.html
2021-03-31 01:06:50 +01:00

316 lines
8.1 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of PTHREAD_SETCANCELSTATE</TITLE>
</HEAD><BODY>
<H1>PTHREAD_SETCANCELSTATE</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-10-10<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
pthread_setcancelstate, pthread_setcanceltype -
set cancelability state and type
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/pthread.h">pthread.h</A>&gt;</B>
<B>int pthread_setcancelstate(int </B><I>state</I><B>, int *</B><I>oldstate</I><B>);</B>
<B>int pthread_setcanceltype(int </B><I>type</I><B>, int *</B><I>oldtype</I><B>);</B>
Compile and link with <I>-pthread</I>.
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>pthread_setcancelstate</B>()
sets the cancelability state of the calling thread to the value
given in
<I>state</I>.
The previous cancelability state of the thread is returned
in the buffer pointed to by
<I>oldstate</I>.
The
<I>state</I>
argument must have one of the following values:
<DL COMPACT>
<DT id="1"><B>PTHREAD_CANCEL_ENABLE</B>
<DD>
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.
<DT id="2"><B>PTHREAD_CANCEL_DISABLE</B>
<DD>
The thread is not cancelable.
If a cancellation request is received,
it is blocked until cancelability is enabled.
</DL>
<P>
The
<B>pthread_setcanceltype</B>()
sets the cancelability type of the calling thread to the value
given in
<I>type</I>.
The previous cancelability type of the thread is returned
in the buffer pointed to by
<I>oldtype</I>.
The
<I>type</I>
argument must have one of the following values:
<DL COMPACT>
<DT id="3"><B>PTHREAD_CANCEL_DEFERRED</B>
<DD>
A cancellation request is deferred until the thread next calls
a function that is a cancellation point (see
<B><A HREF="/cgi-bin/man/man2html?7+pthreads">pthreads</A></B>(7)).
This is the default cancelability type in all new threads,
including the initial thread.
<DT id="4"><DD>
Even with deferred cancellation, a
cancellation point in an asynchronous signal handler may still
be acted upon and the effect is as if it was an asynchronous
cancellation.
<DT id="5"><B>PTHREAD_CANCEL_ASYNCHRONOUS</B>
<DD>
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.)
</DL>
<P>
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.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, these functions return 0;
on error, they return a nonzero error number.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
The
<B>pthread_setcancelstate</B>()
can fail with the following error:
<DL COMPACT>
<DT id="6"><B>EINVAL</B>
<DD>
Invalid value for
<I>state</I>.
</DL>
<P>
The
<B>pthread_setcanceltype</B>()
can fail with the following error:
<DL COMPACT>
<DT id="7"><B>EINVAL</B>
<DD>
Invalid value for
<I>type</I>.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>pthread_setcancelstate</B>(),
<B>pthread_setcanceltype</B>()
</TD><TD>Thread safety</TD><TD>
MT-Safe
<BR></TD></TR>
<TR VALIGN=top><TD>
<B>pthread_setcancelstate</B>(),
<B>pthread_setcanceltype</B>()
</TD><TD>Async-cancel-safety</TD><TD>
AC-Safe
<BR></TD></TR>
</TABLE>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
For details of what happens when a thread is canceled, see
<B><A HREF="/cgi-bin/man/man2html?3+pthread_cancel">pthread_cancel</A></B>(3).
<P>
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.
<A NAME="lbAJ">&nbsp;</A>
<H3>Asynchronous cancelability</H3>
Setting the cancelability type to
<B>PTHREAD_CANCEL_ASYNCHRONOUS</B>
is rarely useful.
Since the thread could be canceled at
<I>any</I>
time, it cannot safely reserve resources (e.g., allocating memory with
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(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
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(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.
<P>
Functions that can be safely asynchronously canceled are called
<I>async-cancel-safe functions</I>.
POSIX.1-2001 and POSIX.1-2008 require only that
<B><A HREF="/cgi-bin/man/man2html?3+pthread_cancel">pthread_cancel</A></B>(3),
<B>pthread_setcancelstate</B>(),
and
<B>pthread_setcanceltype</B>()
be async-cancel-safe.
In general, other library functions
can't be safely called from an asynchronously cancelable thread.
<P>
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.
<A NAME="lbAK">&nbsp;</A>
<H3>Portability notes</H3>
The Linux threading implementations permit the
<I>oldstate</I>
argument of
<B>pthread_setcancelstate</B>()
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</I>
argument,
but POSIX.1 does not specify this point,
so portable applications should always specify a non-NULL value in
<I>oldstate</I>.
A precisely analogous set of statements applies for the
<I>oldtype</I>
argument of
<B>pthread_setcanceltype</B>().
<A NAME="lbAL">&nbsp;</A>
<H2>EXAMPLE</H2>
See
<B><A HREF="/cgi-bin/man/man2html?3+pthread_cancel">pthread_cancel</A></B>(3).
<A NAME="lbAM">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+pthread_cancel">pthread_cancel</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_cleanup_push">pthread_cleanup_push</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_testcancel">pthread_testcancel</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+pthreads">pthreads</A></B>(7)
<A NAME="lbAN">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="8"><A HREF="#lbAB">NAME</A><DD>
<DT id="9"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="10"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="11"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="12"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="13"><A HREF="#lbAG">ATTRIBUTES</A><DD>
<DT id="14"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="15"><A HREF="#lbAI">NOTES</A><DD>
<DL>
<DT id="16"><A HREF="#lbAJ">Asynchronous cancelability</A><DD>
<DT id="17"><A HREF="#lbAK">Portability notes</A><DD>
</DL>
<DT id="18"><A HREF="#lbAL">EXAMPLE</A><DD>
<DT id="19"><A HREF="#lbAM">SEE ALSO</A><DD>
<DT id="20"><A HREF="#lbAN">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:53 GMT, March 31, 2021
</BODY>
</HTML>