|PTHREAD_SETCANCELSTATE(3)||Linux Programmer's Manual||PTHREAD_SETCANCELSTATE(3)|
int pthread_setcancelstate(int state, int *oldstate); int pthread_setcanceltype(int type, int *oldtype);
Compile and link with -pthread.
- 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. The previous cancelability type of the thread is returned in the buffer pointed to by oldtype. The type argument must have one of the following values:
- A cancellation request is deferred until the thread next calls a function that is a cancellation point (see pthreads(7)). This is the default cancelability type in all new threads, including the initial thread.
- 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.
- 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.)
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.
- Invalid value for state.
The pthread_setcanceltype() can fail with the following error:
- Invalid value for type.
|pthread_setcancelstate (), pthread_setcanceltype ()||Thread safety||MT-Safe|
|pthread_setcancelstate (), pthread_setcanceltype ()||Async-cancel safety||AC-Safe|
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.
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.
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.