Manual Pages for Linux CentOS command on man pthread_cancel
MyWebUniversity

Manual Pages for Linux CentOS command on man pthread_cancel

PTHREADCANCEL(3) Linux Programmer's Manual PTHREADCANCEL(3)

NAME

pthreadcancel - send a cancellation request to a thread SYNOPSIS

#include int pthreadcancel(pthreadt thread);

Compile and link with -pthread. DESCRIPTION The pthreadcancel() function sends a cancellation request to the thread thread. Whether and when the target thread reacts to the can‐ cellation request depends on two attributes that are under the control of that thread: its cancelability state and type. A thread's cancelability state, determined by pthreadsetcancel‐ state(3), can be enabled (the default for new threads) or disabled. 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. A thread's cancellation type, determined by pthreadsetcanceltype(3), may be either asynchronous or deferred (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 cancellation point. A list of functions that are or may be cancellation points is provided in pthreads(7). When a cancellation requested is acted on, the following steps occur for thread (in this order):

1. Cancellation clean-up handlers are popped (in the reverse of the order in which they were pushed) and called. (See pthreadcleanuppush(3).)

2. Thread-specific data destructors are called, in an unspecified order. (See pthreadkeycreate(3).) 3. The thread is terminated. (See pthreadexit(3).) The above steps happen asynchronously with respect to the pthreadcan‐ cel() call; the return status of pthreadcancel() merely informs the caller whether the cancellation request was successfully queued. After a canceled thread has terminated, a join with that thread using pthreadjoin(3) obtains PTHREADCANCELED as the thread's exit status. (Joining with a thread is the only way to know that cancellation has completed.) RETURN VALUE On success, pthreadcancel() returns 0; on error, it returns a nonzero error number. ERRORS ESRCH No thread with the ID thread could be found. CONFORMING TO

POSIX.1-2001. NOTES 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 SIGUSR2 is used. EXAMPLE 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 PTHREADCANCELED. The following shell session shows what happens when we run the program:

$ ./a.out threadfunc(): started; cancellation disabled main(): sending cancellation request threadfunc(): about to enable cancellation main(): thread was canceled Program source

#include

#include

#include

#include

#include

#define handleerroren(en, msg) \ do { errno = en; perror(msg); exit(EXITFAILURE); } while (0) static void * threadfunc(void *ignoredargument) { int s; /* Disable cancellation for a while, so that we don't immediately react to a cancellation request */ s = pthreadsetcancelstate(PTHREADCANCELDISABLE, NULL); if (s != 0) handleerroren(s, "pthreadsetcancelstate"); printf("threadfunc(): started; cancellation disabled\n"); sleep(5); printf("threadfunc(): about to enable cancellation\n"); s = pthreadsetcancelstate(PTHREADCANCELENABLE, NULL); if (s != 0) handleerroren(s, "pthreadsetcancelstate"); /* sleep() is a cancellation point */ sleep(1000); /* Should get canceled while we sleep */ /* Should never get here */ printf("threadfunc(): not canceled!\n"); return NULL; } int main(void) { pthreadt thr; void *res; int s; /* Start a thread and then send it a cancellation request */ s = pthreadcreate(&thr, NULL, &threadfunc, NULL); if (s != 0) handleerroren(s, "pthreadcreate"); sleep(2); /* Give thread a chance to get started */ printf("main(): sending cancellation request\n"); s = pthreadcancel(thr); if (s != 0) handleerroren(s, "pthreadcancel"); /* Join with thread to see what its exit status was */ s = pthreadjoin(thr, &res); if (s != 0) handleerroren(s, "pthreadjoin"); if (res == PTHREADCANCELED) printf("main(): thread was canceled\n"); else printf("main(): thread wasn't canceled (shouldn't happen!)\n"); exit(EXITSUCCESS); } SEE ALSO pthreadcleanuppush(3), pthreadcreate(3), pthreadexit(3), pthreadjoin(3), pthreadkeycreate(3), pthreadsetcancelstate(3), pthreadsetcanceltype(3), pthreadtestcancel(3), pthreads(7) COLOPHON

This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can

be found at http://www.kernel.org/doc/man-pages/.

Linux 2008-11-17 PTHREADCANCEL(3)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™