何时确切初始化在全局范围内声明的thread_local变量? [英] When exactly is a thread_local variable declared at global scope initialized?
本文介绍了何时确切初始化在全局范围内声明的thread_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();
}
从此处何时初始化尚不清楚. >
It's still not entirely clear from here when is n initialized.
推荐答案
足够简单,并且都符合规范.每当运行新线程时,n
都将被初始化-在您输入任何特定于线程的函数之前.
Simple enough, and all according to specification. n
is going to be initialized whenever the new thread is run - before you enter any thread-specific functions.
确切地说,它将被初始化三次.
To be exact, it is going to be initialized three times.
这篇关于何时确切初始化在全局范围内声明的thread_local变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文