_Thread_local storage-class specifier. Including the <threads.h> standard library header provides the thread_local convenience macro, which expands to _Thread_local.
Technical Characteristics
Storage Duration and Scope Applyingthread_local alters the storage duration of a variable to thread storage duration. It can be applied to variables at file scope (globals) or block scope (inside functions). According to the C11 standard (6.7.1), if _Thread_local is used on a block-scope declaration, it cannot be used alone; it must be explicitly accompanied by either the static or extern keyword.
Linkage
The linkage of a thread-local variable depends on its declaration context and accompanying specifiers:
- External Linkage: File-scope declarations without the
statickeyword, or block-scope declarations with theexternkeyword. - Internal Linkage: File-scope declarations combined with the
statickeyword. - No Linkage: Block-scope declarations combined with the
statickeyword.
&) to a thread-local variable yields a pointer to the specific instance belonging to the calling thread. While it is syntactically valid to pass this pointer to another thread, doing so requires strict lifetime management; dereferencing the pointer after the owning thread has terminated results in undefined behavior.
Pre-C11 Compiler Extensions
For codebases predating C11 or operating on non-compliant compilers, thread-local storage is typically implemented via compiler-specific extensions rather than standard keywords:Tired of Poor C Skills? Fix That With Deep Grasping!Learn More





