PTHREAD_CANCEL(3POSIX)

πŸ’– PTHREAD_CANCEL πŸ’–

okay bestie, gather round, because we're talking about pthread_cancel β€” that's the pthread_cancel(3POSIX) page, straight out of the POSIX Programmer's Manual. πŸŽ€


✨ PROLOG ✨

so like, real talk first: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β€” so if you're on Linux, go check the corresponding Linux manual page for the deets on Linux behavior. or honestly the interface might not even be implemented on Linux at all. πŸ™ˆ no cap.


🌸 NAME 🌸

pthread_cancel β€” cancel execution of a thread. yes, we are ending a thread's whole career. πŸ’…


⭐ SYNOPSIS ⭐

#include <pthread.h>

int pthread_cancel(pthread_t thread);

πŸ¦‹ DESCRIPTION πŸ¦‹

so the pthread_cancel() function shall request that thread be canceled. key word: request, bestie. it's giving "we asked nicely." ✨

The target thread's cancelability state and type determines when the cancellation actually takes effect. like, the thread gets a say in the timing. icon behavior honestly.

When the cancellation is acted on, the cancellation cleanup handlers for thread shall be called. When the last cancellation cleanup handler returns, the thread-specific data destructor functions shall be called for thread. And when the last destructor function returns, thread shall be terminated. so it's a whole little goodbye sequence β€” cleanup handlers first, then destructors, then poof. 🫢

The cancellation processing in the target thread shall run asynchronously with respect to the calling thread returning from pthread_cancel(). so the caller does not sit there waiting for the whole thing to play out. main character energy: caller says the thing and moves on. πŸ’…


πŸ”₯ RETURN VALUE πŸ”₯

If successful, the pthread_cancel() function shall return zero; otherwise, an error number shall be returned to indicate the error. that's it, that's the vibe check. βœ…


πŸ’€ ERRORS πŸ’€

The pthread_cancel() function shall not return an error code of [EINTR]. noted. do not come for EINTR, it's not invited to this party. πŸ™…

The following sections are informative.


🌈 EXAMPLES 🌈

None. (not me showing up with zero examples 😭 β€” the standard said no.)


πŸ’œ APPLICATION USAGE πŸ’œ

None. (again. iconic of them.)


🧚 RATIONALE 🧚

okay so this is the lore section and it's actually kinda juicy. Two alternative functions were considered for sending the cancellation notification to a thread:

  1. define a new SIGCANCEL signal that had the cancellation semantics when delivered;
  2. define the new pthread_cancel() function, which would trigger the cancellation semantics.

The advantage of a new signal was that so much of the delivery criteria were identical to that used when trying to deliver a signal that making cancellation notification a signal was seen as consistent. Indeed, many implementations implement cancellation using a special signal. 🎯

On the other hand, there would be no signal functions that could be used with this signal except pthread_kill(), and the behavior of the delivered cancellation signal would be unlike any previously existing defined signal. 😬

The benefits of a special function include the recognition that this signal would be defined because of the similar delivery criteria and that this is the only common behavior between a cancellation request and a signal. In addition, the cancellation delivery mechanism does not have to be implemented as a signal. There are also strong, if not stronger, parallels with language exception mechanisms than with signals that are potentially obscured if the delivery mechanism is visibly closer to signals. πŸ‘‘

In the end, it was considered that as there were so many exceptions to the use of the new signal with existing signals functions it would be misleading. A special function has resolved this problem. This function was carefully defined so that an implementation wishing to provide the cancellation functions on top of signals could do so. The special function also means that implementations are not obliged to implement cancellation with signals. periodt. πŸ’…

If an implementation detects use of a thread ID after the end of its lifetime, it is recommended that the function should fail and report an [ESRCH] error. (using a dead thread ID is not the move, bestie β€” that's a yikes. 🚩)


⭐ FUTURE DIRECTIONS ⭐

None.


πŸŽ€ SEE ALSO πŸŽ€

  • pthread_exit()
  • pthread_cond_timedwait()
  • pthread_join()
  • pthread_setcancelstate()

The Base Definitions volume of POSIX.1‐2017, <pthread.h>


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!! πŸ«ΆπŸ’– brought to you by IEEE/The Open Group, dated 2017, filed under PTHREAD_CANCEL(3POSIX) β€” mwah, slay, go cancel responsibly. βœ¨πŸŽ€πŸ¦‹