2016-02-19 76 views
4

例如:在全局范围声明的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(); 
} 

它仍然没有完全从here明确为n初始化时。

+0

线程初始化时。 –

+0

对于每个线程(线程)初始化时。 –

+1

相关:http://stackoverflow.com/questions/24253584/when-is-a-thread-local-global-variable-initialized –

回答

3

够简单了,一切都按规格说明。当你输入任何线程特定的函数之前,每当新线程运行时,n将被初始化。

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