c++ static vs thread storage duration destruction order

北慕城南 提交于 2019-12-24 03:32:12

问题


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

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?


回答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.



来源:https://stackoverflow.com/questions/30200893/c-static-vs-thread-storage-duration-destruction-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!