【发布时间】:2016-02-19 14:52:19
【问题描述】:
例如:
#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();
}
从here 中仍然不完全清楚 n 何时初始化。
【问题讨论】:
-
线程初始化时。
-
对于每个线程(线程)被初始化时。
-
看起来确实有点混乱。 local 是每个线程的 n 的 storage。但是 n 的范围(即它在代码中的可见性)是全局的。
-
初始化本身发生在答案(此处和链接的问题中)描述:在使用之前。具体什么时候?标准未指定。
标签: c++ multithreading initialization storage