NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |ATTRIBUTES |STANDARDS |HISTORY |NOTES |EXAMPLES |SEE ALSO |COLOPHON | |
pthread_s...ncelstate(3) Library Functions Manualpthread_s...ncelstate(3)pthread_setcancelstate, pthread_setcanceltype - set cancelability state and type
POSIX threads library (libpthread,-lpthread)
#include <pthread.h>int pthread_setcancelstate(intstate, int *oldstate);int pthread_setcanceltype(inttype, int *oldtype);
Thepthread_setcancelstate() sets the cancelability state of the calling thread to the value given instate. The previous cancelability state of the thread is returned in the buffer pointed to byoldstate. Thestate argument must have one of the following values:PTHREAD_CANCEL_ENABLE 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 cancelation request.PTHREAD_CANCEL_DISABLE The thread is not cancelable. If a cancelation request is received, it is blocked until cancelability is enabled. Thepthread_setcanceltype() sets the cancelability type of the calling thread to the value given intype. The previous cancelability type of the thread is returned in the buffer pointed to byoldtype. Thetype argument must have one of the following values:PTHREAD_CANCEL_DEFERRED A cancelation request is deferred until the thread next calls a function that is a cancelation point (seepthreads(7)). This is the default cancelability type in all new threads, including the initial thread. Even with deferred cancelation, a cancelation point in an asynchronous signal handler may still be acted upon and the effect is as if it was an asynchronous cancelation.PTHREAD_CANCEL_ASYNCHRONOUS The thread can be canceled at any time. (Typically, it will be canceled immediately upon receiving a cancelation 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.
On success, these functions return 0; on error, they return a nonzero error number.
Thepthread_setcancelstate() can fail with the following error:EINVALInvalid value forstate. Thepthread_setcanceltype() can fail with the following error:EINVALInvalid value fortype.
For an explanation of the terms used in this section, seeattributes(7). ┌────────────────────────────────┬─────────────────────┬─────────┐ │Interface│Attribute│Value│ ├────────────────────────────────┼─────────────────────┼─────────┤ │pthread_setcancelstate(), │ Thread safety │ MT-Safe │ │pthread_setcanceltype() │ │ │ ├────────────────────────────────┼─────────────────────┼─────────┤ │pthread_setcancelstate(), │ Async-cancel safety │ AC-Safe │ │pthread_setcanceltype() │ │ │ └────────────────────────────────┴─────────────────────┴─────────┘
POSIX.1-2008.
glibc 2.0 POSIX.1-2001.
For details of what happens when a thread is canceled, seepthread_cancel(3). Briefly disabling cancelability is useful if a thread performs some critical action that must not be interrupted by a cancelation 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 cancelation requests.Asynchronous cancelability Setting the cancelability type toPTHREAD_CANCEL_ASYNCHRONOUSis rarely useful. Since the thread could be canceled atany time, it cannot safely reserve resources (e.g., allocating memory withmalloc(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 cancelation 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 themalloc(3) family of functions) may be left in an inconsistent state if cancelation occurs in the middle of the function call. Consequently, clean-up handlers cease to be useful. Functions that can be safely asynchronously canceled are calledasync-cancel-safe functions. POSIX.1-2001 and POSIX.1-2008 require only thatpthread_cancel(3),pthread_setcancelstate(), andpthread_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 cancelation of a thread that is in a pure compute-bound loop.Portability notes The Linux threading implementations permit theoldstate argument ofpthread_setcancelstate() to be NULL, in which case the information about the previous cancelability state is not returned to the caller. Many other implementations also permit a NULLoldstat argument, but POSIX.1 does not specify this point, so portable applications should always specify a non-NULL value inoldstate. A precisely analogous set of statements applies for theoldtype argument ofpthread_setcanceltype().
Seepthread_cancel(3).
pthread_cancel(3),pthread_cleanup_push(3),pthread_testcancel(3),pthreads(7)
This page is part of theman-pages (Linux kernel and C library user-space interface documentation) project. Information about the project can be found at ⟨https://www.kernel.org/doc/man-pages/⟩. If you have a bug report for this manual page, see ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩. This page was obtained from the tarball man-pages-6.15.tar.gz fetched from ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on 2025-08-11. If you discover any rendering problems in this HTML version of the page, or you believe there is a better or more up- to-date source for the page, or you have corrections or improvements to the information in this COLOPHON (which isnot part of the original manual page), send a mail to man-pages@man7.orgLinux man-pages 6.15 2025-05-17pthread_s...ncelstate(3)Pages that refer to this page:pthread_cancel(3), pthread_cleanup_push(3), pthread_cleanup_push_defer_np(3), pthread_kill_other_threads_np(3), pthread_testcancel(3), pthreads(7)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |