PTHREAD_CLEANUP_POP_RESTORE_NP(3)
π pthread_cleanup_push_defer_np, pthread_cleanup_pop_restore_np π
okay bestie, so this page lives in the Library Functions Manual, and it's all about the two GLOWED-UP cousins of the classic cancelation cleanup functions: <hotpink>pthread_cleanup_push_defer_np</hotpink> and <hotpink>pthread_cleanup_pop_restore_np</hotpink>. They push and pop thread cancelation clean-up handlers, but the twist? They also save your cancelability type. obsessed. π
β¨ 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, aka <lavender>libpthread</lavender>, link it with <cyan>-lpthread</cyan> π«Ά
π§ 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)):
pthread_cleanup_push_defer_np(),pthread_cleanup_pop_defer_np():_GNU_SOURCE
yes bestie, it really does say pthread_cleanup_pop_defer_np() up there. we're reading it exactly how it's written. no edits, no cap. π
πΈ DESCRIPTION πΈ
so basically, these two are the same as pthread_cleanup_push(3) and pthread_cleanup_pop(3) β the only tea is the differences spelled out on this very page. β¨
pthread_cleanup_push_defer_np() does push routine onto the thread's stack of cancelation clean-up handlers, just like pthread_cleanup_push(3). but wait, there's more π it also saves the thread's current cancelability type, and sets the cancelability type to <hotpink>"deferred"</hotpink> (see pthread_setcanceltype(3)). why, you ask? so that cancelation clean-up is guaranteed to happen even if the thread's cancelability type was <red>"asynchronous"</red> before the call. iconique protective behavior, honestly. π‘οΈ
pthread_cleanup_pop_restore_np() pops the top-most clean-up handler off the thread's stack of cancelation clean-up handlers, just like pthread_cleanup_pop(3). and then β the extra sprinkle π β it restores the thread's cancelability type to whatever it was 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 too, and they're described over in pthread_cleanup_push(3). read that one bestie, fr fr. π
and here's the equivalence moment, because this is the part that ate:
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);
likeβ¦ literally the same energy, less typing, more speed. main character code. π
π STANDARDS π
GNU. which is why the names have that _np suffix β it stands for nonportable π (so don't go using these on a strict POSIX platform and act surprised, iykyk).
π¦ HISTORY π¦
glibc 2.0 β a true vintage. she's been here since the beginning and she's still slaying. β
π SEE ALSO π
pthread_cancel(3)pthread_cleanup_push(3)pthread_setcancelstate(3)pthread_testcancel(3)pthreads(7)
and that's the whole page, bestie!! π this lil rewrite was brought to you by Linux man-pages 6.20, dated 2025-05-17 β go read the real thing too if you want the raw text, but honestly you already got the full vibe check right here. bye pookie, happy threading!! πβ¨π«Ά
π¦ 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