339 lines
11 KiB
HTML
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"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
pthread_cancel - send a cancellation request to a thread
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/pthread.h">pthread.h</A>></B>
|
|
|
|
<B>int pthread_cancel(pthread_t </B><I>thread</I><B>);</B>
|
|
|
|
Compile and link with <I>-pthread</I>.
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </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"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On success,
|
|
<B>pthread_cancel</B>()
|
|
|
|
returns 0;
|
|
on error, it returns a nonzero error number.
|
|
<A NAME="lbAF"> </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"> </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"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008.
|
|
<A NAME="lbAI"> </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"> </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"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/pthread.h">pthread.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/errno.h">errno.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
<P>
|
|
#define handle_error_en(en, msg) \
|
|
<BR> do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
|
|
<P>
|
|
static void *
|
|
thread_func(void *ignored_argument)
|
|
{
|
|
<BR> int s;
|
|
<P>
|
|
<BR> /* Disable cancellation for a while, so that we don't
|
|
<BR> immediately react to a cancellation request */
|
|
<P>
|
|
<BR> s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_setcancelstate");
|
|
<P>
|
|
<BR> printf("thread_func(): started; cancellation disabled\n");
|
|
<BR> <A HREF="/cgi-bin/man/man2html?5+sleep">sleep</A>(5);
|
|
<BR> printf("thread_func(): about to enable cancellation\n");
|
|
<P>
|
|
<BR> s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_setcancelstate");
|
|
<P>
|
|
<BR> /* sleep() is a cancellation point */
|
|
<P>
|
|
<BR> sleep(1000); /* Should get canceled while we sleep */
|
|
<P>
|
|
<BR> /* Should never get here */
|
|
<P>
|
|
<BR> printf("thread_func(): not canceled!\n");
|
|
<BR> return NULL;
|
|
}
|
|
<P>
|
|
int
|
|
main(void)
|
|
{
|
|
<BR> pthread_t thr;
|
|
<BR> void *res;
|
|
<BR> int s;
|
|
<P>
|
|
<BR> /* Start a thread and then send it a cancellation request */
|
|
<P>
|
|
<BR> s = pthread_create(&thr, NULL, &thread_func, NULL);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_create");
|
|
<P>
|
|
<BR> <A HREF="/cgi-bin/man/man2html?2+sleep">sleep</A>(2); /* Give thread a chance to get started */
|
|
<P>
|
|
<BR> printf("main(): sending cancellation request\n");
|
|
<BR> s = pthread_cancel(thr);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_cancel");
|
|
<P>
|
|
<BR> /* Join with thread to see what its exit status was */
|
|
<P>
|
|
<BR> s = pthread_join(thr, &res);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_join");
|
|
<P>
|
|
<BR> if (res == PTHREAD_CANCELED)
|
|
<BR> printf("main(): thread was canceled\n");
|
|
<BR> else
|
|
<BR> printf("main(): thread wasn't canceled (shouldn't happen!)\n");
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAL"> </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"> </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"> </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>
|