【问题标题】:initialization order of thread local variables线程局部变量的初始化顺序
【发布时间】:2019-01-19 08:03:39
【问题描述】:

引自 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;

【问题讨论】:

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


    【解决方案1】:

    来自6.7.2 线程存储时长[basic.stc.thread]

    具有线程存储持续时间的变量应在其第一次使用 odr 之前进行初始化 (6.2),如果已构造, 应在线程退出时销毁。

    这意味着 V 在 `W' 之前被初始化。

    【讨论】:

    • 如果没有依赖又都需要动态初始化怎么办?
    猜你喜欢
    • 2017-05-14
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 2016-09-07
    相关资源
    最近更新 更多