Manpage of PTHREAD_SETCANCELSTATE
PTHREAD_SETCANCELSTATESection: Linux Programmer's Manual (3)
NAMEpthread_setcancelstate, pthread_setcanceltype - set cancelability state and type
#include <pthread.h>int pthread_setcancelstate(int state, int *oldstate);int pthread_setcanceltype(int type, int *oldtype);Compile and link with -pthread.
DESCRIPTIONThe pthread_setcancelstate() sets the cancelability state of the calling thread to the value given in state. The cancelability state of the thread is returned in the buffer pointed to by oldstate. The stateargument must have one of the following values:
- 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.
- The thread is not cancelable. If a cancellation request is received, it is blocked until cancelability is enabled.
The pthread_setcanceltype() sets the cancelability type of the calling thread to the value given in type. Thecancelability type of the thread is returned in the buffer pointed to by oldtype. The typeargument must have one of the following values:
- A cancellation request is deferred until the thread pthreads(7)). This is the default cancelability type in all new threads, including the initial thread. calls a function that is a cancellation point (see
- 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.)
RETURN VALUEOn success, these functions return 0; on error, they return a nonzero error number.
ERRORSThe pthread_setcancelstate() can fail with the following error:
- Invalid value for state.
The pthread_setcanceltype() can fail with the following error:
- Invalid value for type.
ATTRIBUTESFor an explanation of the terms used in this section, see attributes(7).
|pthread_setcancelstate(), pthread_setcanceltype()||Thread safety||MT-Safe
CONFORMING TOPOSIX.1-2001, POSIX.1-2008.
NOTESFor details of what happens when a thread is canceled, see pthread_cancel(3).
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.
Asynchronous cancelabilitySetting the cancelability type to PTHREAD_CANCEL_ASYNCHRONOUSis rarely useful. Since the thread could be canceled at anytime, it cannot safely reserve resources (e.g., allocating memory with 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 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.
Functions that can be safely asynchronously canceled are called async-cancel-safe functions. POSIX.1-2001 and POSIX.1-2008 require only that pthread_cancel(3), pthread_setcancelstate(), and pthread_setcanceltype() be async-cancel-safe. In general, other library functions can't be safely called from an asynchronously cancelable thread.
Portability notesThe Linux threading implementations permit the oldstateargument of pthread_setcancelstate() to be NULL, in which case the information about the cancelability state is not returned to the caller. Many other implementations also permit a NULL oldstatargument, but POSIX.1 does not specify this point, so portable applications should always specify a non-NULL value in oldstate. A precisely analogous set of statements applies for the oldtypeargument of pthread_setcanceltype().
SEE ALSOpthread_cancel(3), pthread_cleanup_push(3), pthread_testcancel(3), pthreads(7)
- RETURN VALUE
- CONFORMING TO
- SEE ALSO
This document was created by man2html, using the manual pages.
Time: 16:30:17 GMT, March 14, 2017 Click Here!