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

initialization order of thread local variables

本文关键字:顺序 初始化 局部变量 线程      更新时间:2023-10-16

引用N4713:

使用静态动态初始化非局部变量 V 和 W 存储持续时间排序如下:
如果 V 和 W 已订购 初始化和 V 在单个转换中的 W 之前定义 单位,V的初始化在初始化之前排序 的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]

具有线程存储持续时间的变量应在首次使用 (6.2( 之前初始化,如果构造, 应在线程退出时销毁。

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