On this page
_Thread_local
Prev: _Atomic | Up: builtins | Next: _Static_assert
Rel: threads.h
Ref: cppreference — _Thread_local
Tags: #public #prog #c #builtins #tier/specialized
Availability
Built-in (Storage-Class Specifier (C11; thread_local in C23)) — requires no #include header.
Syntax & Overview
_Thread_local T x;
Gives each OS thread its own independent instance of a variable for the thread's lifetime.
Notes & Pitfalls
- One independent variable instance per thread (C11;
thread_localin C23): Avoids mutexes for per-thread scratch buffers or error state.
Example
#include <assert.h>
static _Thread_local int last_err = 0;
int main(void) {
last_err = 42;
assert(last_err == 42);
}
Linked from
Related notes
- tuts/c/builtins/_Atomic Linked here; links to this page; shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/_Static_assert Linked here; links to this page; shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins Linked here; links to this page; shared tags: #builtins, #c, #prog
- tuts/c/stdlib/threads Linked here; shared tags: #c, #prog
- tuts/c/builtins/__FILE__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/__func__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/__LINE__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/__STDC_VERSION__ shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/_Alignof shared tags: #builtins, #c, #prog, #tier
- tuts/c/builtins/_Generic shared tags: #builtins, #c, #prog, #tier