What lifetime, initialization, and destruction guarantees does thread_local provide, and what costs can it introduce?
Show answer and explanation
A thread_local variable denotes a distinct object for each thread and has thread storage duration. Block-scope thread_local initialization occurs when control first passes its declaration in that thread; non-trivial constructed objects are destroyed when that thread exits, with namespace-scope initialization and cross-unit dependencies still requiring careful ordering. Addresses from one thread must not outlive or be used after that thread's object is destroyed. TLS access, guarded initialization, per-thread memory, and exit-time destruction can be material costs, especially with many short-lived threads, so thread-local state should not replace explicit ownership without measurement.
A thread_local variable denotes a distinct object for each thread and has thread storage duration. Block-scope thread_local initialization occurs when control first passes its declaration in that thread; non-trivial constructed objects are destroyed when that thread exits, with namespace-scope initialization and cross-unit dependencies still requiring careful ordering. Addresses from one thread must not outlive or be used after that thread's object is destroyed. TLS access, guarded initialization, per-thread memory, and exit-time destruction can be material costs, especially with many short-lived threads, so thread-local state should not replace explicit ownership without measurement.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.