c++ - 线程局部变量的初始化顺序

标签 c++ c++11 initialization thread-local thread-local-storage

引用自 N4713:

Dynamic initialization of non-local variables V and W with static storage duration are ordered as follows:
If V and W have ordered initialization and V is defined before W within a single translation unit, the initialization of V is sequenced before the initialization of W.

线程存储持续时间的变量有类似的段落吗?下面的代码中,是在初始化之前排序的V的初始化 W

thread_local int V = 1;
thread_local int W = V;

更新:

我想修改代码如下:

thread_local string V;
thread_local string W;

最佳答案

来自6.7.2线程存储持续时间[basic.stc.thread]:

A variable with thread storage duration shall be initialized before its first odr-use (6.2) and, if constructed, shall be destroyed on thread exit.

这意味着 V 在“W”之前初始化。

关于c++ - 线程局部变量的初始化顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51807963/

相关文章:

c++ - 尝试访问 it 属性时前向声明 'const class LHContactInfo' 错误

c++ - 打包结构和不打包没有代码重复

c++ - 根据数据成员排序或过滤对象数组

c++ - 确定执行循环所需的 CPU 时间

c++ - 使用迭代器将数组分成大小不等的部分

c - 初始化结构指针

java - 出于什么目的,人们想要在同一类的 main 中创建一个类的实例?

c++ - 通用引用 : Cannot convert parameter from 'int' to 'int &&'

c++ - 在 O(1) 中访问模板参数包的背面

arrays - 如何快速初始化我的数组