PTHREAD_SETCANCELSTATE(3P)
π PTHREAD_SETCANCELSTATE(3P) π
okay bestie, so this is a page from the POSIX Programmer's Manual β yes, that manual. settle in.
π¨ PROLOG π¨
so like, real talk: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (go check the corresponding Linux manual page for the deets on Linux behavior, fr), or honestly the interface may not even be implemented on Linux at all. iykyk. π
β¨ NAME β¨
pthread_setcancelstate, pthread_setcanceltype, pthread_testcancel β set cancelability state. that's the whole vibe. π
πΈ SYNOPSIS πΈ
#include <pthread.h>
int pthread_setcancelstate(int state, int *oldstate);
int pthread_setcanceltype(int type, int *oldtype);
void pthread_testcancel(void);
π DESCRIPTION π
okay so here's the tea, function by function:
pthread_setcancelstate() β this one shall atomically do two things at once (iconic behavior honestly): set the calling thread's cancelability state to the indicated state, AND hand you back the previous cancelability state at the location referenced by oldstate. the legal values for state are PTHREAD_CANCEL_ENABLE and PTHREAD_CANCEL_DISABLE. that's it, that's the menu. π«Ά
pthread_setcanceltype() β same energy, different setting. it shall atomically set the calling thread's cancelability type to the indicated type and return the previous cancelability type at the location referenced by oldtype. legal values for type are PTHREAD_CANCEL_DEFERRED and PTHREAD_CANCEL_ASYNCHRONOUS. β¨
the default vibe check β every newly created thread, including the thread in which main() was first invoked (yes, that one, the main character), starts out with cancelability state PTHREAD_CANCEL_ENABLE and cancelability type PTHREAD_CANCEL_DEFERRED. respectively. no cap. π
pthread_testcancel() β this one shall create a cancellation point in the calling thread. but here's the thing bestie: it shall have no effect if cancelability is disabled. like, it's just sitting there. politely. doing nothing. π
π RETURN VALUE π
if everything's slaying: pthread_setcancelstate() and pthread_setcanceltype() shall return zero. otherwise, an error number shall be returned to indicate the error. that's the deal. π
π ERRORS π
pthread_setcancelstate() may fail if:
EINVALβ the specifiedstateis notPTHREAD_CANCEL_ENABLEorPTHREAD_CANCEL_DISABLE.
pthread_setcanceltype() may fail if:
EINVALβ the specifiedtypeis notPTHREAD_CANCEL_DEFERREDorPTHREAD_CANCEL_ASYNCHRONOUS.
also important, don't @ me: these functions shall not return an error code of [EINTR]. periodt.
the following sections are informative. (yes, that's a real line, we're keeping it, it's giving β¨structureβ¨)
π EXAMPLES π
None.
...yeah. that's it. nothing. an empty plate. a cancelled example, if you will. π
π§ APPLICATION USAGE π§
okay so this is the important bit for the signal-handler girlies:
in order to write a signal handler for an asynchronous signal which can run safely in a cancellable thread, pthread_setcancelstate() must be used to disable cancellation for the duration of any calls that the signal handler makes which are cancellation points. slay, right?
BUT. π
the standard does not permit strictly conforming applications to call pthread_setcancelstate() from a signal handler, since it is not currently required to be async-signal-safe. so like, don't do that, it's not it.
on implementations where pthread_setcancelstate() is not async-signal-safe, your alternatives are:
- ensure that the corresponding signals are blocked during execution of functions that are not async-cancel-safe, OR
- ensure that cancellation is disabled during times when those signals could be delivered.
and implementations? be so fr right now β they are strongly encouraged to make pthread_setcancelstate() async-signal-safe. we're manifesting it. π―οΈβ¨
π¦ RATIONALE π¦
so pthread_setcancelstate() and pthread_setcanceltype() control the points at which a thread may be asynchronously canceled. for cancellation control to be usable in modular fashion, some rules need to be followed. and bestie, we are following them.
an object can be considered to be a generalization of a procedure. it is a set of procedures and global variables written as a unit and called by clients not known by the object. objects may depend on other objects. very nesting-doll of them. πͺ
rule one: cancelability should only be disabled on entry to an object, never explicitly enabled. on exit from an object, the cancelability state should always be restored to its value on entry to the object.
why? modularity argument, here's the logic: if the client of an object (or the client of an object that uses that object) has disabled cancelability, it's because the client does not want to be concerned about cleaning up if the thread is canceled while executing some sequence of actions. if an object is called in such a state and it enables cancelability and a cancellation request is pending for that thread, then the thread is canceled β contrary to the wish of the client that disabled it. and that's just rude. π
rule two: the cancelability type may be explicitly set to either deferred or asynchronous upon entry to an object. but, as with the cancelability state, on exit from an object the cancelability type should always be restored to its value on entry to the object. put it back where you found it, babe. π
rule three (the final one, the closer): only functions that are cancel-safe may be called from a thread that is asynchronously cancelable. that's the boundary. respect it. π
β FUTURE DIRECTIONS β
pthread_setcancelstate() may be added to the table of async-signal-safe functions in Section 2.4.3, Signal Actions. so... watch this space. potential glow-up incoming. π₯
π SEE ALSO π
pthread_cancel()- The Base Definitions volume of POSIX.1β2017,
<pthread.h>
π COPYRIGHT π
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie!! π«Ά this little masterpiece came to you courtesy of IEEE/The Open Group, 2017 β aka PTHREAD_SETCANCELSTATE(3P). go forth and cancel responsibly. πβ¨ππ¦
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- 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-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms