thread_local 对象是否在 C 中初始化为 0?

Are thread_local objects initialized to 0 in C?

非本地线程存储持续时间对象在C中是否初始化为0?

static thread_local int x; // is x initialized to 0?

C17 标准如下(在 6.2.4.4 中)。

An object whose identifier is declared with the storage-class specifier _Thread_local has thread storage duration. Its lifetime is the entire execution of the thread for which it is created, and its stored value is initialized when the thread is started.

没有明确说初始化为0,但是C++ 标准明确表示:(在 3.6.2.2 中)

Variables with static storage duration ([basic.stc.static]) or thread storage duration ([basic.stc.thread]) shall be zero-initialized ([dcl.init]) before any other initialization takes place

那么,C 是否将非本地 thread_local 对象初始化为 0?

没有显式初始化的线程局部对象被初始化为“零”。

C 2018 7.26.1 3 表示如果包含 <threads.h>,则 thread_local 扩展为 _Thread_local。问题中没有显示,但大概是这样。

C 2018 6.2.4 4 说“其标识符声明为 storage-class 说明符 _Thread_local 的对象具有 线程存储持续时间

C 2018 6.7.9 10 说:

… If an object that has static or thread storage duration is not initialized explicitly, then:

— if it has pointer type, it is initialized to a null pointer;

— if it has arithmetic type, it is initialized to (positive or unsigned) zero;

— if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

— if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

该段到此结束;尾随的“;”而不是句点是印刷错误。