Thread-Local Storage
Thread-local storage gives each thread its own instance of a variable. Writes in one thread do not race with writes in another, because they are different objects.
Detailed Explanation
C11 _Thread_local (and C23 thread_local, and the GCC __thread extension) declares a variable with thread storage duration. Each thread that touches it gets a copy, zero-initialized or with the initializer you wrote, as if the program had a separate static for that thread.
errno is the existence proof. A failed open in thread A must not clobber thread B's errno. The C library implements that with TLS.
TLS is not a reason to avoid passing context. A thread_local buffer in a library function makes the function non-reentrant in a different way: it is safe across threads and unsafe if the same thread calls it recursively or from a signal handler. strtok is the historical lesson; strtok_r takes the state as a parameter instead.
POSIX also has pthread_key_create, pthread_setspecific, and a destructor that runs when a thread exits. That API can attach a heap block to a thread and free it automatically. _Thread_local pointers do not run a destructor; if you store a malloc pointer in TLS, you still have to free it.
Dynamic linking and TLS interact. A dlopened library that uses TLS has to be supported by the loader; very old platforms had surprises. For ordinary application variables, _Thread_local static in a .c file is fine.
Do not use TLS as a hidden argument to skip an API change in a public library if the function might be used from many threads with many contexts in one thread (think thread pools running unrelated jobs). Pass a struct Context *.
Code Examples
#include <stdio.h>
#include <pthread.h>
static _Thread_local int mine;
static void *worker(void *arg)
{
mine = *(int *)arg;
printf("%d\n", mine);
return NULL;
}
int main(void)
{
mine = 1;
int two = 2;
pthread_t t;
pthread_create(&t, NULL, worker, &two);
pthread_join(t, NULL);
printf("%d\n", mine); /* prints: 1 (this thread's copy) */
return 0;
}#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
static pthread_key_t key;
static void free_buf(void *p)
{
free(p);
}
static void *worker(void *arg)
{
(void)arg;
char *buf = malloc(8);
if (buf == NULL) {
return NULL;
}
buf[0] = 'x';
buf[1] = '\0';
pthread_setspecific(key, buf);
printf("%s\n", (char *)pthread_getspecific(key)); /* prints: x */
return NULL; /* destructor frees buf */
}
int main(void)
{
pthread_key_create(&key, free_buf);
pthread_t t;
pthread_create(&t, NULL, worker, NULL);
pthread_join(t, NULL);
pthread_key_delete(key);
return 0;
}Common Gotchas
A `thread_local` pointer does not free the pointee when the thread exits
use a pthread key destructor or free it yourself.
TLS does not make a function safe to call twice on the same thread with overlapping use of the buffer
`strtok` is still broken under recursion.
Taking the address of a TLS variable gives a pointer that is only valid in that thread
passing it to another thread is a dangling pointer for that thread's copy.
Initializing TLS from another thread's pointer to it is a lifetime bug
each thread has its own object.