PTHREAD_GETSPECIFIC(3P)
π PTHREAD_GETSPECIFIC(3P) π
okay bestie, welcome to the POSIX Programmer's Manual, we're doing thread-specific data management today and honestly? it's giving β¨organized chaosβ¨
π«Ά PROLOG π«Ά
so like, this manual page is part of the POSIX Programmer's Manual, periodt. but heads up: the Linux implementation of this interface may differ (so go consult the corresponding Linux manual page for the tea on Linux behavior), or the interface might not even be implemented on Linux at all. π no cap.
πΈ NAME πΈ
pthread_getspecific, pthread_setspecific β thread-specific data management, and yes they're besties, they come as a pair π«Ά
β¨ SYNOPSIS β¨
#include <pthread.h>
void *pthread_getspecific(pthread_key_t key);
int pthread_setspecific(pthread_key_t key, const void *value);
π DESCRIPTION π
okay so first up, pthread_getspecific() shall return the value currently bound to the specified key on behalf of the calling thread. that's it, that's the whole job. iconic.
then pthread_setspecific() shall associate a thread-specific value with a key obtained via a previous call to pthread_key_create(). the way different threads may bind different values to the same key?? love that for them π these values are typically pointers to blocks of dynamically allocated memory that have been reserved for use by the calling thread.
now the scary part bestie, pay attention: the effect of callingpthread_getspecific()orpthread_setspecific()with a key value not obtained frompthread_key_create(), or after key has been deleted withpthread_key_delete(), is undefined. undefined. we do not do that. π
both pthread_getspecific() and pthread_setspecific() may be called from a thread-specific data destructor function. a call to pthread_getspecific() for the thread-specific data key being destroyed shall return the value NULL, unless the value is changed (after the destructor starts) by a call to pthread_setspecific(). and calling pthread_setspecific() from a thread-specific data destructor routine may result either in lost storage (after at least PTHREAD_DESTRUCTOR_ITERATIONS attempts at destruction) or in an infinite loop. π yes really. be careful.
also, both functions may be implemented as macros. lowkey sneaky but okay. π¦
π RETURN VALUE π
pthread_getspecific()shall return the thread-specific data value associated with the given key. if no thread-specific data value is associated with key, then the valueNULLshall be returned. simple as that πpthread_setspecific()β if successful, it shall return zero; otherwise, an error number shall be returned to indicate the error.
π ERRORS π
so pthread_getspecific()? no errors are returned from it. none. she's flawless, she doesn't do errors. π
the pthread_setspecific() function shall fail if:
ENOMEMβ insufficient memory exists to associate the non-NULLvalue with the key. π the storage said no.
also, pthread_setspecific() shall not return an error code of [EINTR]. nope, not happening.
the following sections are informative. (iykyk, they're the vibes section, not the rules section)
π EXAMPLES π
None. π
π¦ APPLICATION USAGE π¦
None. β¨
π§ RATIONALE π§
okay so here's the reasoning bestie: performance and ease-of-use of pthread_getspecific() are critical for functions that rely on maintaining state in thread-specific data. since no errors are required to be detected by it, and since the only error that could be detected is the use of an invalid key, the function to pthread_getspecific() has been designed to favor speed and simplicity over error reporting. she's fast, she's simple, she doesn't snitch. periodt. π₯
and if an implementation detects that the value specified by the key argument to pthread_setspecific() does not refer to a a key value obtained from pthread_key_create() or refers to a key that has been deleted with pthread_key_delete(), it is recommended that the function should fail and report an [EINVAL] error. (yes, the original says "a a", we are preserving that typo with love π)
β FUTURE DIRECTIONS β
None. nothing's changing, we're stable, we're at peace π«Ά
πΈ SEE ALSO πΈ
pthread_key_create()- 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 one came from IEEE/The Open Group, dated 2017, and it's the PTHREAD_GETSPECIFIC(3P) girlies' official page β thanks for reading, mwah, go forth and manage your thread-specific data 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