0

I have a function that is called from multiple threads:

void func()
{
   static MyObject someobject;
   someobject.complexFunc();
}

At program termination, can I assume that someobject is destroyed after the last thread has been terminated? If no, how can I make sure threads don't access it anymore when say std::exit is called?

Thanks

Desperado17
  • 835
  • 6
  • 12
  • 1
    I don't think the answers to the duplicate address this question. – Brian Bi Jun 20 '19 at 15:14
  • You can assume that someobject is destroyed after the last thread has been terminated **if** you shut down all the running threads before your `main()` returns. After `main()` returns, the `atexit()` chain will be executed, which is where the static objects are destructed. But if threads are left running, and leveraging global statics or scoped statics (such as the example), then those threads may step into UB during program termination, which can do who-knows-what because it is UB. – Eljay Jun 20 '19 at 15:36
  • Is this c++11 behavior or was static lifetime guaranteed until main returns even before that? – Desperado17 Jun 20 '19 at 21:18

0 Answers0