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

# pthread\_cleanup\_pop\_restore\_np(3)

push and pop thread cancelation clean-up handlers while saving cancelability type

Part of [pthread\_cleanup\_push\_defer\_np(3)](/pthread_cleanup_push_defer_np/3.md).

## NAME

pthread\_cleanup\_push\_defer\_np, pthread\_cleanup\_pop\_restore\_np - push and pop thread cancelation clean-up handlers while saving cancelability type

## LIBRARY

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

## SYNOPSIS

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

**void pthread\_cleanup\_push\_defer\_np(typeof(void (void \*)) \****routine***,**                                    **void \****arg***);** **void pthread\_cleanup\_pop\_restore\_np(int** *execute***);**

Feature Test Macro Requirements for glibc (see [**feature\_test\_macros**(7)](/feature_test_macros/7.md)):

**pthread\_cleanup\_push\_defer\_np**(), **pthread\_cleanup\_pop\_defer\_np**():

```
    _GNU_SOURCE
```

## DESCRIPTION

These functions are the same as [**pthread\_cleanup\_push**(3)](/pthread_cleanup_push/3.md) and [**pthread\_cleanup\_pop**(3)](/pthread_cleanup_pop/3.md), except for the differences noted on this page.

Like [**pthread\_cleanup\_push**(3)](/pthread_cleanup_push/3.md), **pthread\_cleanup\_push\_defer\_np**() pushes *routine* onto the thread's stack of cancelation clean-up handlers. In addition, it also saves the thread's current cancelability type, and sets the cancelability type to "deferred" (see [**pthread\_setcanceltype**(3)](/pthread_setcanceltype/3.md)); this ensures that cancelation clean-up will occur even if the thread's cancelability type was "asynchronous" before the call.

Like [**pthread\_cleanup\_pop**(3)](/pthread_cleanup_pop/3.md), **pthread\_cleanup\_pop\_restore\_np**() pops the top-most clean-up handler from the thread's stack of cancelation clean-up handlers. In addition, it restores the thread's cancelability type to its value at the time of the matching **pthread\_cleanup\_push\_defer\_np**().

The caller must ensure that calls to these functions are paired within the same function, and at the same lexical nesting level. Other restrictions apply, as described in [**pthread\_cleanup\_push**(3)](/pthread_cleanup_push/3.md).

This sequence of calls:

```
pthread_cleanup_push_defer_np(routine, arg);
pthread_cleanup_pop_restore_np(execute);
```

is equivalent to (but shorter and more efficient than):

```
int oldtype;
pthread_cleanup_push(routine, arg);
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
...
pthread_setcanceltype(oldtype, NULL);
pthread_cleanup_pop(execute);
```

## STANDARDS

GNU; hence the suffix "\_np" (nonportable) in the names.

## HISTORY

glibc 2.0

## SEE ALSO

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