pthread_key_create(3)
π pthread_key_create(3) π
okay bestie, listen. this one is from the Library Functions Manual (yes that's the manual name, we're in section 3, the library call girlies), and it's literally about managing thread-specific data. i know, i know, but stay with me, it's actually so good.
β¨ NAME β¨
pthread_key_create, pthread_key_delete, pthread_setspecific, pthread_getspecific β management of thread-specific data
like, the whole TSD friend group. four besties, one mission. π
π SYNOPSIS π
#include <pthread.h>
int pthread_key_create(pthread_key_t *key,
typeof(void (void *)) *destr_function;
int pthread_key_delete(pthread_key_t key);
int pthread_setspecific(pthread_key_t key, const void *pointer);
void * pthread_getspecific(pthread_key_t key);
πΈ DESCRIPTION πΈ
so here's the situation, bestie. programs often need global or static variables that have different values in different threads. but girl. threads share one memory space. so you literally cannot do this with regular variables. thread-specific data is the POSIX threads answer to this need. it's giving "we made a whole subsystem about it." iconic π«Ά
each thread possesses a private memory block, the thread-specific data area, or TSD area for short. this area is indexed by TSD keys. the TSD area associates values of type void * to TSD keys. TSD keys are common to all threads, but the value associated with a given TSD key can be different in each thread. so the keys are shared, the values are private. main character energy for real.
for concreteness β and i love a concrete visual β the TSD areas can be viewed as arrays of void * pointers, TSD keys as integer indices into these arrays, and the value of a TSD key as the value of the corresponding array element in the calling thread. yeah. it's an array. it was always an array. π
when a thread is created, its TSD area initially associates NULL with all keys.
pthread_key_create()β allocates a new TSD key. the key is stored in the location pointed to bykey. there is a limit ofPTHREAD_KEYS_MAXon the number of keys allocated at a given time. and the value initially associated with the returned key isNULLin all currently executing threads. fresh key, blank slate, no baggage, we love that for her.destr_functionβ the destructor function argument. if it's notNULL, it specifies a destructor function associated with the key. when a thread terminates viapthread_exit()or by cancelation,destr_functionis called with arguments the value associated with the key in that thread. thedestr_functionis not called if that value isNULL. and the order in which destructor functions are called at thread termination time is unspecified β so don't come for me, it's not defined, that's just the law.
okay so before the destructor function is called, theNULLvalue is associated with the key in the current thread. a destructor function might, however, re-associate non-NULLvalues to that key or some other key. sneaky! to deal with this, if after all the destructors have been called for all non-NULLvalues, there are still some non-NULLvalues with associated destructors, then the process is repeated. the glibc implementation stops the process afterPTHREAD_DESTRUCTOR_ITERATIONSiterations, even if some non-NULLvalues with associated descriptors remain. other implementations may loop indefinitely. so like... know your libc. fr fr.
pthread_key_delete()β deallocates a TSD key. it does not check whether non-NULLvalues are associated with that key in the currently executing threads, nor call the destructor function associated with the key. it just. deletes. no goodbyes. no closure. πpthread_setspecific()β changes the value associated withkeyin the calling thread, storing the givenpointerinstead.pthread_getspecific()β returns the value currently associated withkeyin the calling thread. your little lookup bestie.
β RETURN VALUE β
pthread_key_create(), pthread_key_delete(), and pthread_setspecific() return 0 on success and a non-zero error code on failure. if successful, pthread_key_create() stores the newly allocated key in the location pointed to by its key argument. so check the return value, bestie, always. π«Ά
pthread_getspecific() returns the value associated with key on success, and NULL on error.
π₯ ERRORS π₯
pthread_key_create() returns the following error code on error:
EAGAINβPTHREAD_KEYS_MAXkeys are already allocated. you hit the ceiling, girl. no more keys. the key rack is full.
pthread_key_delete() and pthread_setspecific() return the following error code on error:
EINVALβkeyis not a valid, allocated TSD key.
pthread_getspecific() returns NULL if key is not a valid, allocated TSD key.
π¦ SEE ALSO π¦
pthread_create(3), pthread_exit(3), pthread_testcancel(3).
π EXAMPLE π
okay this example ate, honestly. the following code fragment allocates a thread-specific array of 100 characters, with automatic reclamation at thread exit. automatic cleanup?? we're obsessed.
/* Key for the thread-specific buffer */
static pthread_key_t buffer_key;
/* Once-only initialisation of the key */
static pthread_once_t buffer_key_once = PTHREAD_ONCE_INIT;
/* Allocate the thread-specific buffer */
void buffer_alloc(void)
{
pthread_once(&buffer_key_once, buffer_key_alloc);
pthread_setspecific(buffer_key, malloc(100));
}
/* Return the thread-specific buffer */
char * get_buffer(void)
{
return (char *) pthread_getspecific(buffer_key);
}
/* Allocate the key */
static void buffer_key_alloc()
{
pthread_key_create(&buffer_key, buffer_destroy);
}
/* Free the thread-specific buffer */
static void buffer_destroy(void * buf)
{
free(buf);
}
and that's the whole page, bestie π this one came to you from Linux man-pages 6.20, dated 2025-05-17. go forth and key your threads responsibly. no cap. π β¨
π¦ 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