| 1 |
.\" Copyright, Xavier Leroy <[email protected]> |
| 2 |
.\" Copyright 2023, Alejandro Colomar <[email protected]> |
| 3 |
.\" |
| 4 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 5 |
.\" |
| 6 |
.TH pthread_key_create 3 2024-06-16 "Linux man-pages 6.9.1" |
| 7 |
. |
| 8 |
. |
| 9 |
.SH NAME |
| 10 |
pthread_key_create, |
| 11 |
pthread_key_delete, |
| 12 |
pthread_setspecific, |
| 13 |
pthread_getspecific |
| 14 |
\- |
| 15 |
management of thread-specific data |
| 16 |
. |
| 17 |
. |
| 18 |
.SH SYNOPSIS |
| 19 |
.nf |
| 20 |
.B #include <pthread.h> |
| 21 |
.P |
| 22 |
.BI "int pthread_key_create(pthread_key_t *" key , |
| 23 |
.BI " void (*" destr_function ") (void *));" |
| 24 |
.BI "int pthread_key_delete(pthread_key_t " key ); |
| 25 |
.BI "int pthread_setspecific(pthread_key_t " key ", const void *" pointer ); |
| 26 |
.BI "void * pthread_getspecific(pthread_key_t " key ); |
| 27 |
.fi |
| 28 |
. |
| 29 |
. |
| 30 |
.SH DESCRIPTION |
| 31 |
Programs often need global or static variables |
| 32 |
that have different values in different threads. |
| 33 |
Since threads share one memory space, |
| 34 |
this cannot be achieved with regular variables. |
| 35 |
Thread-specific data is the POSIX threads answer to this need. |
| 36 |
.P |
| 37 |
Each thread possesses a private memory block, |
| 38 |
the thread-specific data area, |
| 39 |
or TSD area for short. |
| 40 |
This area is indexed by TSD keys. |
| 41 |
The TSD area associates values of type \fBvoid *\fP to TSD keys. |
| 42 |
TSD keys are common to all threads, |
| 43 |
but the value associated with a given TSD key |
| 44 |
can be different in each thread. |
| 45 |
.P |
| 46 |
For concreteness, |
| 47 |
the TSD areas can be viewed as arrays of \fBvoid *\fP pointers, |
| 48 |
TSD keys as integer indices into these arrays, |
| 49 |
and the value of a TSD key |
| 50 |
as the value of the corresponding array element in the calling thread. |
| 51 |
.P |
| 52 |
When a thread is created, |
| 53 |
its TSD area initially associates \fBNULL\fP with all keys. |
| 54 |
.P |
| 55 |
\fBpthread_key_create\fP allocates a new TSD key. |
| 56 |
The key is stored in the location pointed to by \fIkey\fP. |
| 57 |
There is a limit of \fBPTHREAD_KEYS_MAX\fP |
| 58 |
on the number of keys allocated at a given time. |
| 59 |
The value initially associated with the returned key |
| 60 |
is \fBNULL\fP in all currently executing threads. |
| 61 |
.P |
| 62 |
The \fIdestr_function\fP argument, |
| 63 |
if not \fBNULL\fP, |
| 64 |
specifies a destructor function associated with the key. |
| 65 |
When a thread terminates via \fBpthread_exit\fP or by cancelation, |
| 66 |
\fIdestr_function\fP is called with arguments |
| 67 |
the value associated with the key in that thread. |
| 68 |
The \fIdestr_function\fP is not called if that value is \fBNULL\fP. |
| 69 |
The order in which destructor functions are called at thread termination time |
| 70 |
is unspecified. |
| 71 |
.P |
| 72 |
Before the destructor function is called, |
| 73 |
the \fBNULL\fP value is associated with the key in the current thread. |
| 74 |
A destructor function might, |
| 75 |
however, |
| 76 |
re-associate non-\fBNULL\fP values to that key or some other key. |
| 77 |
To deal with this, |
| 78 |
if after all the destructors have been called |
| 79 |
for all non-\fBNULL\fP values, |
| 80 |
there are still some non-\fBNULL\fP values with associated destructors, |
| 81 |
then the process is repeated. |
| 82 |
The glibc implementation stops the process |
| 83 |
after \fBPTHREAD_DESTRUCTOR_ITERATIONS\fP iterations, |
| 84 |
even if some non-\fBNULL\fP values with associated descriptors remain. |
| 85 |
Other implementations may loop indefinitely. |
| 86 |
.P |
| 87 |
\fBpthread_key_delete\fP deallocates a TSD key. |
| 88 |
It does not check |
| 89 |
whether non-\fBNULL\fP values are associated with that key |
| 90 |
in the currently executing threads, |
| 91 |
nor call the destructor function associated with the key. |
| 92 |
.P |
| 93 |
\fBpthread_setspecific\fP changes the value |
| 94 |
associated with \fIkey\fP in the calling thread, |
| 95 |
storing the given \fIpointer\fP instead. |
| 96 |
.P |
| 97 |
\fBpthread_getspecific\fP returns the value |
| 98 |
currently associated with \fIkey\fP in the calling thread. |
| 99 |
. |
| 100 |
. |
| 101 |
.SH "RETURN VALUE" |
| 102 |
\fBpthread_key_create\fP, |
| 103 |
\fBpthread_key_delete\fP, |
| 104 |
and \fBpthread_setspecific\fP |
| 105 |
return 0 on success and a non-zero error code on failure. |
| 106 |
If successful, |
| 107 |
\fBpthread_key_create\fP stores the newly allocated key |
| 108 |
in the location pointed to by its \fIkey\fP argument. |
| 109 |
.P |
| 110 |
\fBpthread_getspecific\fP returns |
| 111 |
the value associated with \fIkey\fP on success, |
| 112 |
and \fBNULL\fP on error. |
| 113 |
. |
| 114 |
. |
| 115 |
.SH ERRORS |
| 116 |
\fBpthread_key_create\fP returns the following error code on error: |
| 117 |
.RS |
| 118 |
.TP |
| 119 |
\fBEAGAIN\fP |
| 120 |
\fBPTHREAD_KEYS_MAX\fP keys are already allocated. |
| 121 |
.RE |
| 122 |
.P |
| 123 |
\fBpthread_key_delete\fP and \fBpthread_setspecific\fP return |
| 124 |
the following error code on error: |
| 125 |
.RS |
| 126 |
.TP |
| 127 |
\fBEINVAL\fP |
| 128 |
\fIkey\fP is not a valid, allocated TSD key. |
| 129 |
.RE |
| 130 |
.P |
| 131 |
\fBpthread_getspecific\fP returns \fBNULL\fP if \fIkey\fP is not a valid, |
| 132 |
allocated TSD key. |
| 133 |
. |
| 134 |
. |
| 135 |
.SH "SEE ALSO" |
| 136 |
pthread_create(3), pthread_exit(3), pthread_testcancel(3). |
| 137 |
. |
| 138 |
. |
| 139 |
.SH EXAMPLE |
| 140 |
The following code fragment |
| 141 |
allocates a thread-specific array of 100 characters, |
| 142 |
with automatic reclamation at thread exit: |
| 143 |
.P |
| 144 |
.RS |
| 145 |
.ft 3 |
| 146 |
.nf |
| 147 |
.sp |
| 148 |
/* Key for the thread-specific buffer */ |
| 149 |
static pthread_key_t buffer_key; |
| 150 |
\& |
| 151 |
/* Once-only initialisation of the key */ |
| 152 |
static pthread_once_t buffer_key_once = PTHREAD_ONCE_INIT; |
| 153 |
\& |
| 154 |
/* Allocate the thread-specific buffer */ |
| 155 |
void buffer_alloc(void) |
| 156 |
{ |
| 157 |
pthread_once(&buffer_key_once, buffer_key_alloc); |
| 158 |
pthread_setspecific(buffer_key, malloc(100)); |
| 159 |
} |
| 160 |
\& |
| 161 |
/* Return the thread-specific buffer */ |
| 162 |
char * get_buffer(void) |
| 163 |
{ |
| 164 |
return (char *) pthread_getspecific(buffer_key); |
| 165 |
} |
| 166 |
\& |
| 167 |
/* Allocate the key */ |
| 168 |
static void buffer_key_alloc() |
| 169 |
{ |
| 170 |
pthread_key_create(&buffer_key, buffer_destroy); |
| 171 |
} |
| 172 |
\& |
| 173 |
/* Free the thread-specific buffer */ |
| 174 |
static void buffer_destroy(void * buf) |
| 175 |
{ |
| 176 |
free(buf); |
| 177 |
} |
| 178 |
.ft |
| 179 |
.RE |
| 180 |
.fi |