lundi 21 janvier 2019

Detached threads accessing global or static objects

The following text is an excerpt taken from the section 18.2.1 of the book titled The C++ Standard Library: A Tutorial and Reference, 2nd Edition:

Note, however, that the lifetime problem also applies to global and static objects, because when the program exits, the detached thread might still run, which means that it might access global or static objects that are already destroyed or under destruction. Unfortunately, this would result in undefined behavior.

As far as I understand, all detached threads will terminate when main() ends.

Therefore, I suspect that the reason for this behaviour is that the actual order of destruction of the global and static objects is unspecified with respect to detached threads' termination, i.e., it may happen before, during, or after the detached threads terminate.

Any further clarification on this matter would be appreciated.


To be more specific: in the subsection under the title Beware of Detached Threads.

Aucun commentaire:

Enregistrer un commentaire