c ++ static vs thread存储持续时间销毁顺序

时间:2022-01-13 01:55:09

Consider how in c++ there are these two storage duration (among others): static storage duration and thread storage duration..

考虑如何在c ++中存在这两个存储持续时间(以及其他):静态存储持续时间和线程存储持续时间。

Next consider this code:

接下来考虑以下代码:

static MyClassA a;
thread_local static MyClassB b;

Additional pretend that "a" and "b" might not be in the same compilation unit. I "believe" that the destructor of "b" will be called before "a" as the thread storage duration will terminate first and only after that is complete will the static storage duration terminate and call the destructor of "a". I've been looking for a standard reference to this but I have been unable to find one. Can someone confirm this specifically with an authoritative source?

另外假装“a”和“b”可能不在同一个编译单元中。我“相信”“b”的析构函数将在“a”之前调用,因为线程存储持续时间将首先终止,并且只有在完成后,静态存储持续时间才会终止并调用“a”的析构函数。我一直在寻找标准参考,但我一直找不到。有人可以通过权威来源明确证实这一点吗?

1 个解决方案

#1


[basic.start.term]/p1:

Destructors for initialized objects with thread storage duration within a given thread are called as a result of returning from the initial function of that thread and as a result of that thread calling std::exit. The completions of the destructors for all initialized objects with thread storage duration within that thread are sequenced before the initiation of the destructors of any object with static storage duration.

具有给定线程内的线程存储持续时间的初始化对象的析构函数被调用作为从该线程的初始函数返回的结果,并且作为该线程调用std :: exit的结果。在具有静态存储持续时间的任何对象的析构函数的启动之前,对具有该线程内的线程存储持续时间的所有初始化对象的析构函数的完成进行排序。

#1


[basic.start.term]/p1:

Destructors for initialized objects with thread storage duration within a given thread are called as a result of returning from the initial function of that thread and as a result of that thread calling std::exit. The completions of the destructors for all initialized objects with thread storage duration within that thread are sequenced before the initiation of the destructors of any object with static storage duration.

具有给定线程内的线程存储持续时间的初始化对象的析构函数被调用作为从该线程的初始函数返回的结果,并且作为该线程调用std :: exit的结果。在具有静态存储持续时间的任何对象的析构函数的启动之前,对具有该线程内的线程存储持续时间的所有初始化对象的析构函数的完成进行排序。