> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C Thread-Local Variable

A thread-local variable in C is a variable with thread storage duration, meaning each thread in a multithreaded program receives its own independent, isolated instance of the variable. The memory for the variable is allocated when a thread is created and automatically deallocated when that specific thread terminates.

Introduced in the C11 standard, thread-local variables are declared using the `_Thread_local` storage-class specifier. Including the `<threads.h>` standard library header provides the `thread_local` convenience macro, which expands to `_Thread_local`.

```c theme={"dark"}
#include <threads.h>

// Using the C11 keyword directly
_Thread_local int thread_state;

// Using the macro from <threads.h>
thread_local double thread_metric = 0.0;
```

## Technical Characteristics

**Storage Duration and Scope**
Applying `thread_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 `static` keyword, or block-scope declarations with the `extern` keyword.
* **Internal Linkage:** File-scope declarations combined with the `static` keyword.
* **No Linkage:** Block-scope declarations combined with the `static` keyword.

```c theme={"dark"}
#include <threads.h>

// External linkage, thread storage duration
thread_local int global_t_var = 10;

// Internal linkage, thread storage duration
static thread_local int internal_t_var = 20;

void process_data() {
    // No linkage, thread storage duration
    // Must include 'static' (or 'extern') at block scope
    static thread_local int block_t_var = 30; 
}
```

**Initialization Rules**
In C, thread-local variables must be initialized with a constant expression. Initialization occurs at thread startup (C11 Standard 6.2.4), before the thread executes any code. For the initial (main) thread, initialization occurs at program startup. Unlike C++, C does not perform lazy initialization upon the first evaluation of the variable. If no explicit initializer is provided, the variable is implicitly zero-initialized.

**Addressability and Pointers**
Applying the address-of operator (`&`) 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:

```c theme={"dark"}
// GCC / Clang / Intel
__thread int gcc_thread_var;

// Microsoft Visual C++ (MSVC)
__declspec(thread) int msvc_thread_var;
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
