Last Updated: June 6, 2026
Some state should not be shared between threads, even when the variable lives at file or global scope. A request ID, a random number generator, a scratch buffer reused across calls: these are per-thread by nature, and sharing them produces either incorrect results or a queue of threads contending on a mutex. The thread_local keyword, added in C++11, gives every thread its own independent copy of a variable, with a lifetime that matches the thread itself. This chapter covers how thread_local storage works, where it is useful in everyday concurrent code, and the corner cases.