簡體   English   中英

究竟何時初始化在全局范圍內聲明的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();
}

它仍然是不完全清楚,從這里為n初始化時。

很簡單,並且全部符合規范。 每當運行新線程時, n都會被初始化-在您輸入任何特定於線程的函數之前。

確切地說,它將被初始化三次。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM