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

339 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of PTHREAD_CANCEL</TITLE>
</HEAD><BODY>
<H1>PTHREAD_CANCEL</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<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_cancel - send a cancellation request to a thread
<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_cancel(pthread_t </B><I>thread</I><B>);</B>
Compile and link with <I>-pthread</I>.
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>pthread_cancel</B>()
function sends a cancellation request to the thread
<I>thread</I>.
Whether and when the target thread
reacts to the cancellation request depends on
two attributes that are under the control of that thread:
its cancelability
<I>state</I>
and
<I>type</I>.
<P>
A thread's cancelability state, determined by
<B><A HREF="/cgi-bin/man/man2html?3+pthread_setcancelstate">pthread_setcancelstate</A></B>(3),
can be
<I>enabled</I>
(the default for new threads) or
<I>disabled</I>.
If a thread has disabled cancellation,
then a cancellation request remains queued until the thread
enables cancellation.
If a thread has enabled cancellation,
then its cancelability type determines when cancellation occurs.
<P>
A thread's cancellation type, determined by
<B><A HREF="/cgi-bin/man/man2html?3+pthread_setcanceltype">pthread_setcanceltype</A></B>(3),
may be either
<I>asynchronous</I>
or
<I>deferred</I>
(the default for new threads).
Asynchronous cancelability
means that the thread can be canceled at any time
(usually immediately, but the system does not guarantee this).
Deferred cancelability means that cancellation will be delayed until
the thread next calls a function that is a
<I>cancellation point</I>.
A list of functions that are or may be cancellation points is provided in
<B><A HREF="/cgi-bin/man/man2html?7+pthreads">pthreads</A></B>(7).
<P>
When a cancellation requested is acted on, the following steps occur for
<I>thread</I>
(in this order):
<DL COMPACT>
<DT id="1">1.<DD>
Cancellation clean-up handlers are popped
(in the reverse of the order in which they were pushed) and called.
(See
<B><A HREF="/cgi-bin/man/man2html?3+pthread_cleanup_push">pthread_cleanup_push</A></B>(3).)
<DT id="2">2.<DD>
Thread-specific data destructors are called,
in an unspecified order.
(See
<B><A HREF="/cgi-bin/man/man2html?3+pthread_key_create">pthread_key_create</A></B>(3).)
<DT id="3">3.<DD>
The thread is terminated.
(See
<B><A HREF="/cgi-bin/man/man2html?3+pthread_exit">pthread_exit</A></B>(3).)
</DL>
<P>
The above steps happen asynchronously with respect to the
<B>pthread_cancel</B>()
call;
the return status of
<B>pthread_cancel</B>()
merely informs the caller whether the cancellation request
was successfully queued.
<P>
After a canceled thread has terminated,
a join with that thread using
<B><A HREF="/cgi-bin/man/man2html?3+pthread_join">pthread_join</A></B>(3)
obtains
<B>PTHREAD_CANCELED</B>
as the thread's exit status.
(Joining with a thread is the only way to know that cancellation
has completed.)
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success,
<B>pthread_cancel</B>()
returns 0;
on error, it returns a nonzero error number.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
<DL COMPACT>
<DT id="4"><B>ESRCH</B>
<DD>
No thread with the ID
<I>thread</I>
could be found.
</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_cancel</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
On Linux, cancellation is implemented using signals.
Under the NPTL threading implementation,
the first real-time signal (i.e., signal 32) is used for this purpose.
On LinuxThreads, the second real-time signal is used,
if real-time signals are available, otherwise
<B>SIGUSR2</B>
is used.
<A NAME="lbAJ">&nbsp;</A>
<H2>EXAMPLE</H2>
The program below creates a thread and then cancels it.
The main thread joins with the canceled thread to check
that its exit status was
<B>PTHREAD_CANCELED</B>.
The following shell session shows what happens when we run the program:
<P>
$ ./a.out
thread_func(): started; cancellation disabled
main(): sending cancellation request
thread_func(): about to enable cancellation
main(): thread was canceled
<A NAME="lbAK">&nbsp;</A>
<H3>Program source</H3>
#include &lt;<A HREF="file:///usr/include/pthread.h">pthread.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/errno.h">errno.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
<P>
#define handle_error_en(en, msg) \
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do&nbsp;{&nbsp;errno&nbsp;=&nbsp;en;&nbsp;perror(msg);&nbsp;exit(EXIT_FAILURE);&nbsp;}&nbsp;while&nbsp;(0)
<P>
static void *
thread_func(void *ignored_argument)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;s;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Disable&nbsp;cancellation&nbsp;for&nbsp;a&nbsp;while,&nbsp;so&nbsp;that&nbsp;we&nbsp;don't
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;immediately&nbsp;react&nbsp;to&nbsp;a&nbsp;cancellation&nbsp;request&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_setcancelstate(PTHREAD_CANCEL_DISABLE,&nbsp;NULL);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_setcancelstate&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;thread_func():&nbsp;started;&nbsp;cancellation&nbsp;disabled\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;<A HREF="/cgi-bin/man/man2html?5+sleep">sleep</A>(5);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;thread_func():&nbsp;about&nbsp;to&nbsp;enable&nbsp;cancellation\n&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&nbsp;NULL);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_setcancelstate&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;sleep()&nbsp;is&nbsp;a&nbsp;cancellation&nbsp;point&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;sleep(1000);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Should&nbsp;get&nbsp;canceled&nbsp;while&nbsp;we&nbsp;sleep&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Should&nbsp;never&nbsp;get&nbsp;here&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;thread_func():&nbsp;not&nbsp;canceled!\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;NULL;
}
<P>
int
main(void)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pthread_t&nbsp;thr;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;void&nbsp;*res;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;s;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Start&nbsp;a&nbsp;thread&nbsp;and&nbsp;then&nbsp;send&nbsp;it&nbsp;a&nbsp;cancellation&nbsp;request&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_create(&amp;thr,&nbsp;NULL,&nbsp;&amp;thread_func,&nbsp;NULL);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_create&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;<A HREF="/cgi-bin/man/man2html?2+sleep">sleep</A>(2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Give&nbsp;thread&nbsp;a&nbsp;chance&nbsp;to&nbsp;get&nbsp;started&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;main():&nbsp;sending&nbsp;cancellation&nbsp;request\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_cancel(thr);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_cancel&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Join&nbsp;with&nbsp;thread&nbsp;to&nbsp;see&nbsp;what&nbsp;its&nbsp;exit&nbsp;status&nbsp;was&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_join(thr,&nbsp;&amp;res);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_join&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(res&nbsp;==&nbsp;PTHREAD_CANCELED)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;main():&nbsp;thread&nbsp;was&nbsp;canceled\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;else
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;main():&nbsp;thread&nbsp;wasn't&nbsp;canceled&nbsp;(shouldn't&nbsp;happen!)\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAL">&nbsp;</A>
<H2>SEE ALSO</H2>
<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_create">pthread_create</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_exit">pthread_exit</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_join">pthread_join</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_key_create">pthread_key_create</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_setcancelstate">pthread_setcancelstate</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+pthread_setcanceltype">pthread_setcanceltype</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="lbAM">&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="5"><A HREF="#lbAB">NAME</A><DD>
<DT id="6"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="7"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="8"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="9"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="10"><A HREF="#lbAG">ATTRIBUTES</A><DD>
<DT id="11"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="12"><A HREF="#lbAI">NOTES</A><DD>
<DT id="13"><A HREF="#lbAJ">EXAMPLE</A><DD>
<DL>
<DT id="14"><A HREF="#lbAK">Program source</A><DD>
</DL>
<DT id="15"><A HREF="#lbAL">SEE ALSO</A><DD>
<DT id="16"><A HREF="#lbAM">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>