---
name: "pthread_cleanup_push"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/pthread_cleanup_push"
---

# pthread\_cleanup\_push(3)

add a cleanup function for thread exit

## NAME

**pthread\_cleanup\_push** – add a cleanup function for thread exit

## LIBRARY

POSIX Threads Library (libpthread, -lpthread)

## SYNOPSIS

**#include \<pthread.h\>**

*void*\

**pthread\_cleanup\_push**(*void \*cleanup\_routinevoid \**, *void \*arg*);

## DESCRIPTION

The **pthread\_cleanup\_push**() function adds *cleanup\_routine* to the top of the stack of cleanup handlers that get called when the current thread exits.

When *cleanup\_routine* is called, it is passed *arg* as its only argument.

The **pthread\_cleanup\_push**() function is implemented as a macro that opens a new block. Invocations of this function must appear as standalone statements that are paired with a later call of [pthread\_cleanup\_pop(3)](/pthread_cleanup_pop/3.md) in the same lexical scope.

## RETURN VALUES

The **pthread\_cleanup\_push**() function does not return any value.

## ERRORS

None

## SEE ALSO

[pthread\_cleanup\_pop(3)](/pthread_cleanup_pop/3.md), [pthread\_exit(3)](/pthread_exit/3.md)

## STANDARDS

The **pthread\_cleanup\_push**() function conforms to ISO/IEC 9945-1:1996 (“POSIX.1”).
