G++ 4.6 -std=gnu++0x: Static Local Variable Constructor Call Timing and Thread Safety

后端 未结 2 1323
暖寄归人
暖寄归人 2021-01-12 18:46
void a() { ... }
void b() { ... }

struct X
{
    X() { b(); }
};

void f()
{
    a();
    static X x;
    ...
}

Assume f is called multiple times

2条回答
  •  旧巷少年郎
    2021-01-12 19:20

    As far as I know it is guaranteed that b is only called once. However, it is not guaranteed that the initialisation is performed thread safe, which means another thread could potentially work with a half/not initialized x. (That's kind of funny because static mutexes are basicly useless this way.)

提交回复
热议问题