在全局范围内声明的 thread_local 变量究竟何时初始化?
When exactly is a thread_local variable declared at global scope initialized?
例如:
#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 什么时候初始化还不是很清楚
足够简单,并且完全符合规范。 n
将在新线程为 运行 时初始化 - 在您输入任何特定于线程的函数之前。
准确的说是要初始化3次
例如:
#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 什么时候初始化还不是很清楚
足够简单,并且完全符合规范。 n
将在新线程为 运行 时初始化 - 在您输入任何特定于线程的函数之前。
准确的说是要初始化3次