C++ 在全局范围内声明的thread_局部变量何时初始化?

C++ 在全局范围内声明的thread_局部变量何时初始化?,c++,multithreading,initialization,storage,C++,Multithreading,Initialization,Storage,例如: #include <thread> thread_local int n = 1; void f() { ++n; // is n initialized here for each thread or prior to entering f()? } int main() { std::thread ta(f); std::thread tb(f); ta.join(); tb.join(); } #包括 线程_local

例如:

#include <thread>

thread_local int n = 1;

void f()
{
    ++n; // is n initialized here for each thread or prior to entering f()?
}

int main()
{
    std::thread ta(f);
    std::thread tb(f);

    ta.join();
    tb.join();
}
#包括
线程_local int n=1;
void f()
{
++n、 //是在这里为每个线程初始化n还是在输入f()之前初始化n?
}
int main()
{
标准:螺纹ta(f);
std::线程tb(f);
ta.join();
tb.join();
}

还不完全清楚n是何时初始化的。

足够简单,而且都符合规范<代码>n将在运行新线程时初始化-在您输入任何特定于线程的函数之前


确切地说,它将被初始化三次。

当线程被初始化时。对于每个线程,当它(线程)被初始化时。相关:它看起来确实有点混乱。本地的是每个线程n的存储。但是n的范围(即它在代码中的可见性)是全局的。初始化本身发生在答案(这里和链接的问题)描述的:在使用之前。具体什么时候?本标准未规定。