On this page
  1. _Thread_local
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

_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

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