C ++静态与线程存储持续时间销毁顺序 [英] c++ static vs thread storage duration destruction order

查看:88
本文介绍了C ++静态与线程存储持续时间销毁顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

接下来考虑以下代码:

static MyClassA a;
thread_local static MyClassB b;

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

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?

推荐答案

[basic.start.term]/p1:

[basic.start.term]/p1:

具有线程存储持续时间的初始化对象的析构函数 在给定线程中被称为从的返回结果 该线程的初始功能以及该线程调用的结果 std::exit.所有已初始化的析构函数的完成 在该线程内具有线程存储持续时间的对象被排序 在初始化任何具有static的对象的析构函数之前 存储期限.

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.

这篇关于C ++静态与线程存储持续时间销毁顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆