pthread_cleanup_push(3)
β¨ NAME β¨
pthread_cleanup_push, pthread_cleanup_pop β bestie, these push and pop your thread cancelation clean-up handlers π
so this is from the pthread_cleanup_push(3) page, in the Library Functions Manual section, and it's all about the POSIX threads library, okay? let's get into it π«Ά
π LIBRARY π
POSIX threads library (libpthread, -lpthread) π
πΈ SYNOPSIS πΈ
#include <pthread.h>
void pthread_cleanup_push(typeof(void (void *)) *routine, void *arg);
void pthread_cleanup_pop(int execute);
π¦ DESCRIPTION π¦
okay so. these two functions manipulate the calling thread's stack of thread-cancelation clean-up handlers. and a clean-up handler is just a function that gets automatically executed when a thread is canceled (or in various other circumstances, which we WILL get to, don't worry bestie); it might, for example, unlock a mutex so that it becomes available to other threads in the process. very demure, very mindful, very unlocking-your-mutex-on-the-way-out π
pthread_cleanup_push() pushes routine onto the top of the stack of clean-up handlers. when routine is later invoked, it will be given arg as its argument. periods π
pthread_cleanup_pop() removes the routine at the top of the stack of clean-up handlers, and optionally executes it if execute is nonzero. (so like, it'll pop it either way but only call it if execute isn't zero, iykyk π)
a cancelation clean-up handler is popped from the stack and executed in the following circumstances:
- when a thread is canceled, ALL of the stacked clean-up handlers are popped and executed in the reverse of the order in which they were pushed onto the stack. yes bestie, LIFO, it's giving stack energy, it's giving last-in-first-out, it's giving iconic π₯
- when a thread terminates by calling
pthread_exit(3), all clean-up handlers are executed as described in the preceding point. (clean-up handlers are not called if the thread terminates by performing areturnfrom the thread start function. NOT. watch out π) - when a thread calls
pthread_cleanup_pop()with a nonzero execute argument, the top-most clean-up handler is popped and executed.
POSIX.1 permits pthread_cleanup_push() and pthread_cleanup_pop() to be implemented as macros that expand to text containing { and }, respectively. so for this reason, the caller must ensure that calls to these functions are paired within the same function, and at the same lexical nesting level. (in other words, a clean-up handler is established only during the execution of a specified section of code.) pair them up bestie, don't be chaotic π
β οΈ callinglongjmp(3)(siglongjmp(3)) produces undefined results if any call has been made topthread_cleanup_push()orpthread_cleanup_pop()without the matching call of the pair since the jump buffer was filled bysetjmp(3)(sigsetjmp(3)). likewise, callinglongjmp(3)(siglongjmp(3)) from inside a clean-up handler produces undefined results unless the jump buffer was also filled bysetjmp(3)(sigsetjmp(3)) inside the handler. undefined results are NOT the vibe, do not do this π
π RETURN VALUE π
these functions do not return a value. nothing. nada. zero returns, zero problems π
π ERRORS π
there are no errors. literally none. flawless, no notes, we love a function that just works β¨
π ATTRIBUTES π
for an explanation of the terms used in this section, see attributes(7).
- Interface:
pthread_cleanup_push(),pthread_cleanup_pop()β Attribute: Thread safety β Value:MT-Safe
MT-Safe babes, we ARE the safe thread π
π VERSIONS π
on glibc, the pthread_cleanup_push() and pthread_cleanup_pop() functions are implemented as macros that expand to text containing { and }, respectively. this means that variables declared within the scope of paired calls to these functions will be visible within only that scope. so like, your little local variables stay local, no leaking, we love boundaries π«Ά
POSIX.1 says that the effect of using return, break, continue, or goto to prematurely leave a block bracketed pthread_cleanup_push() and pthread_cleanup_pop() is undefined. mobile applications should avoid doing this. wait, no β portable applications should avoid doing this. anyway, don't dip out of the block early bestie, the braces will be SO mad at you π
β STANDARDS β
POSIX.1-2008. certified, standardized, iconic π
π§ HISTORY π§
POSIX.1-2001. glibc 2.0. ancient. vintage. still slaying π
π EXAMPLES π
the program below provides a simple example of the use of the functions described in this page (which is this page, the one you're reading right now, hi πΈ). the program creates a thread that executes a loop bracketed by pthread_cleanup_push() and pthread_cleanup_pop(). this loop increments a global variable, cnt, once each second. depending on what command-line arguments are supplied, the main thread sends the other thread a cancelation request, or sets a global variable that causes the other thread to exit its loop and terminate normally (by doing a return).
in the following shell session, the main thread sends a cancelation request to the other thread:
$ ./a.out;
New thread started
cnt = 0
cnt = 1
Canceling thread
Called clean-up handler
Thread was canceled; cnt = 0
from the above, we see that the thread was canceled, and that the cancelation clean-up handler was called and it reset the value of the global variable cnt to 0. the clean-up handler said "not today" and did its job πͺ
in the next run, the main program sets a global variable that causes other thread to terminate normally:
$ ./a.out x;
New thread started
cnt = 0
cnt = 1
Thread terminated normally; cnt = 2
from the above, we see that the clean-up handler was not executed (because cleanup_pop_arg was 0), and therefore the value of cnt was not reset. no clean-up, no reset, no cap π§’
in the next run, the main program sets a global variable that causes the other thread to terminate normally, and supplies a nonzero value for cleanup_pop_arg:
$ ./a.out x 1;
New thread started
cnt = 0
cnt = 1
Called clean-up handler
Thread terminated normally; cnt = 0
in the above, we see that although the thread was not canceled, the clean-up handler was executed, because the argument given to pthread_cleanup_pop() was nonzero. plot twist: not canceled, still cleaned up π₯
π Program source π
#include <err.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
static int done = 0;
static int cleanup_pop_arg = 0;
static int cnt = 0;
static void
cleanup_handler(void *arg)
{
printf("Called clean-up handler\n");
cnt = 0;
}
static void *
thread_start(void *arg)
{
time_t curr;
printf("New thread started\n");
pthread_cleanup_push(cleanup_handler, NULL);
curr = time(NULL);
while (!done) {
pthread_testcancel(); /* A cancelation point */
if (curr < time(NULL)) {
curr = time(NULL);
printf("cnt = %d\n", cnt); /* A cancelation point */
cnt++;
}
}
pthread_cleanup_pop(cleanup_pop_arg);
return NULL;
}
int
main(int argc, char *argv[])
{
pthread_t thr;
int s;
void *res;
s = pthread_create(&thr, NULL, thread_start, NULL);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_create");
sleep(2); /* Allow new thread to run a while */
if (argc > 1) {
if (argc > 2)
cleanup_pop_arg = atoi(argv[2]);
done = 1;
} else {
printf("Canceling thread\n");
s = pthread_cancel(thr);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_cancel");
}
s = pthread_join(thr, &res);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_join");
if (res == PTHREAD_CANCELED)
printf("Thread was canceled; cnt = %d\n", cnt);
else
printf("Thread terminated normally; cnt = %d\n", cnt);
exit(EXIT_SUCCESS);
}
π«Ά SEE ALSO π«Ά
pthread_cancel(3), pthread_cleanup_push_defer_np(3), pthread_setcancelstate(3), pthread_testcancel(3), pthreads(7)
okay bestie that's the whole page, you ate it, you read it, congrats π this was pthread_cleanup_push(3) from Linux man-pages 6.20, dated 2025-08-20 β go push some clean-up handlers and stay safe out there, ily, periodt πβ¨ππ¦
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms