问题
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