引用自 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/