gpt4 book ai didi

c++ - thread_local 与全局变量的初始化顺序

转载 作者:行者123 更新时间:2023-12-01 12:45:53 26 4
gpt4 key购买 nike

C.h:

#include <iostream>

class C {
public:
explicit C(int id) { std::cout<<"Initialized "<<id<<"\n"; }
};

1.cpp:
#include "C.h"

C global(1);

2.cpp:
#include "C.h"

thread_local C thread(2);

int main() {}

我的问题是:是否保证 global将在 thread 之前初始化?

C++ 标准在这一点上有些含糊,据我所知。它说(来自 C++17 n4659 草案):

[basic.start.static] Static initialization

Variables with static storage duration are initialized as a consequence of program initiation. Variables with thread storage duration are initialized as a consequence of thread execution.



按理说,“程序启动”发生在“线程执行”之前,但由于这两个表达式仅出现在标准中的那个地方,我正在寻求实际语言律师的建议。

最佳答案

我将使用 C++20 工作草案,因为那里的措辞更简洁一些,尽管真正的规则没有改变。
一、thread_local行为基本上像 static就非本地而言:[basic.stc.thread]/2 :

[ Note: A variable with thread storage duration is initialized as specified in [basic.start.static], [basic.start.dynamic], and [stmt.dcl] and, if constructed, is destroyed on thread exit ([basic.start.term]). — end note ]


是的,是笔记。但是声明了一个非本地对象 thread_local基本上是 static所以这是有道理的。
现在,没有 global也不是 thread具有常量初始化 - 所以两者都是零初始化,然后它们必须进行动态初始化。致 [basic.start.dynamic] !

Dynamic initialization of a non-local variable with static storage duration is unordered if the variable is an implicitly or explicitly instantiated specialization, is partially-ordered if the variable is an inline variable that is not an implicitly or explicitly instantiated specialization, and otherwise is ordered.


我们的变量都不是特化的,也都不是内联的。所以两者都被订购了。

A declaration D is appearance-ordered before a declaration E if

  • D appears in the same translation unit as E, or
  • the translation unit containing E has an interface dependency on the translation unit containing D,

in either case prior to E.


我们的声明彼此之间不是按外观顺序排列的。

Dynamic initialization of non-local variables V and W with static storage duration are ordered as follows:


好的,子项目 1:

If V and W have ordered initialization and the definition of V is appearance-ordered before the definition of W, or if V has partially-ordered initialization, W does not have unordered initialization, and for every definition E of W there exists a definition D of V such that D is appearance-ordered before E,


不适用。这是一个复杂的条件,但它并不适用。

Otherwise, if the program starts a thread other than the main thread before either V or W is initialized, it is unspecified in which threads the initializations of V and W occur; the initializations are unsequenced if they occur in the same thread.


不,没有线程。

Otherwise, the initializations of V and W are indeterminately sequenced.


我们走了。 globalthread是不确定的排序。

另请注意:

It is implementation-defined whether the dynamic initialization of a non-local inline variable with static storage duration is sequenced before the first statement of main or is deferred.


和:

It is implementation-defined whether the dynamic initialization of a non-local non-inline variable with thread storage duration is sequenced before the first statement of the initial function of a thread or is deferred.

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

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