pthread_setcancelstate(3) Library Functions Manual pthread_setcancelstate(3) pthread_setcancelstate, pthread_setcanceltype - POSIX threads library (libpthread, -lpthread) #include int pthread_setcancelstate(int state, int *oldstate); int pthread_setcanceltype(int type, int *oldtype); pthread_setcancelstate() state. , oldstate. state : 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. pthread_setcanceltype() type. , oldstate. type : PTHREAD_CANCEL_DEFERRED A cancelation request is deferred until the thread next calls a function that is a cancelation point (see pthreads(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.) (set-and-get), , , . 0; . pthread_setcancelstate() : EINVAL state. pthread_setcanceltype() : EINVAL type. attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |pthread_setcancelstate(), | | 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, see pthread_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. Setting the cancelability type to PTHREAD_CANCEL_ASYNCHRONOUS is rarely useful. Since the thread could be canceled at any time, 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 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 the malloc(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. , async-cancel-safe. POSIX.1-2001 POSIX.1-2008 , pthread_cancel(3), pthread_setcancelstate() pthread_setcanceltype(). , . 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. Linux oldstate pthread_setcancelstate() NULL; . NULL oldstat, POSIX.1 , oldstate , NULL. oldtype pthread_setcanceltype(). pthread_cancel(3). pthread_cancel(3), pthread_cleanup_push(3), pthread_testcancel(3), pthreads(7) () Alexey, Azamat Hackimov , kogamatranslator49 , Darima Kogan , Max Is , Yuri Kozlov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.9.1 2 2024 . pthread_setcancelstate(3)