gpt4 book ai didi

c++ - 究竟什么时候初始化在全局范围内声明的 thread_local 变量?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:28:34 26 4
gpt4 key购买 nike

例如:

#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次

关于c++ - 究竟什么时候初始化在全局范围内声明的 thread_local 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35508133/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com