---
name: "pthread_setcancelstate"
section: "3"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/pthread_setcancelstate"
---

# pthread\_setcancelstate(3)

set cancelability state and type

## NAME

pthread\_setcancelstate, pthread\_setcanceltype - set cancelability state and type

## LIBRARY

POSIX threads library (*libpthread*, *-lpthread*)

## SYNOPSIS

```
#include <pthread.h>
```

**int pthread\_setcancelstate(int** *state***, int \****oldstate***);** **int pthread\_setcanceltype(int** *type***, int \****oldtype***);**

## DESCRIPTION

The **pthread\_setcancelstate**() sets the cancelability state of the calling thread to the value given in *state*. The previous cancelability state of the thread is returned in the buffer pointed to by *oldstate*. The *state* 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.

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:

- **PTHREAD\_CANCEL\_DEFERRED**

  A cancelation request is deferred until the thread next calls a function that is a cancelation point (see [**pthreads**(7)](/pthreads/7.md)). 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.

## RETURN VALUE

On success, these functions return 0; on error, they return a nonzero error number.

## ERRORS

The **pthread\_setcancelstate**() can fail with the following error:

- **EINVAL**

  Invalid value for *state*.

The **pthread\_setcanceltype**() can fail with the following error:

- **EINVAL**

  Invalid value for *type*.

## ATTRIBUTES

For an explanation of the terms used in this section, see [**attributes**(7)](/attributes/7.md).

| | | |
| --- | --- | --- |
| **Interface** | **Attribute** | **Value** |
| **pthread\_setcancelstate**(), **pthread\_setcanceltype**() | Thread safety | MT-Safe |
| **pthread\_setcancelstate**(), **pthread\_setcanceltype**() | Async-cancel safety | AC-Safe |

## STANDARDS

POSIX.1-2008.

## HISTORY

glibc 2.0 POSIX.1-2001.

## NOTES

For details of what happens when a thread is canceled, see [**pthread\_cancel**(3)](/pthread_cancel/3.md).

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 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)](/malloc/3.md)), 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)](/malloc/3.md) 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 called *async-cancel-safe functions*. POSIX.1-2001 and POSIX.1-2008 require only that [**pthread\_cancel**(3)](/pthread_cancel/3.md), **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 cancelation of a thread that is in a pure compute-bound loop.

### Portability notes

The Linux threading implementations permit the *oldstate* argument of **pthread\_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 NULL *oldstat* argument, 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 *oldtype* argument of **pthread\_setcanceltype**().

## EXAMPLES

See [**pthread\_cancel**(3)](/pthread_cancel/3.md).

## SEE ALSO

[**pthread\_cancel**(3)](/pthread_cancel/3.md), [**pthread\_cleanup\_push**(3)](/pthread_cleanup_push/3.md), [**pthread\_testcancel**(3)](/pthread_testcancel/3.md), [**pthreads**(7)](/pthreads/7.md)
