C99中局部静态数组的线程安全

Thread safety of local static array in C99

以下是线程安全的,因为每个数组元素仅由一个线程访问(包括此处未显示的真实世界部分):

static bool myArray[THREAD_COUNT] = {false}; // Only used in DoSomething()

void DoSomething(uint8_t threadIndex)
{
   myArray[threadIndex] = true;
   // Real world function is more complex
}

现在考虑以下代码:

void DoSomething(uint8_t threadIndex)
{
   static bool myArray[THREAD_COUNT] = {false};
   myArray[threadIndex] = true;
   // Real world function is more complex
}

这个函数也是线程安全的吗(特别是考虑到在第一次调用函数时发生的数组初始化,而不是在启动时)?

很安全。所有具有静态存储持续时间的对象都在 程序启动之前初始化。这意味着甚至在任何线程发挥作用之前。

5.1.2 Execution environments:

Two execution environments are defined: freestanding and hosted. In both cases, program startup occurs when a designated C function is called by the execution environment. All objects with static storage duration shall be initialized (set to their initial values) before program startup. The manner and timing of such initialization are otherwise unspecified. Program termination returns control to the execution environment.

(强调我的)。

C99 没有线程的概念。但这就是我从标准中解释上述引述的方式。