Posix Standard
Posix does not seem to determine if atexit handlers are called before or after threads exit .
There are two (or three) ways to complete the normal process.
All threads terminate. When the last thread exits, either by returning or by calling pthread_exit , atexit handlers are executed. In this case, there are no other threads. (It depends on the platform. Some platforms may terminate other threads if the main thread terminates differently than exit , others do not.)
One thread calls exit . In this case, atexit handlers will be executed, and all threads will be completed. Posix does not indicate in which order.
main returns. This is more or less equivalent to calling exit() as the last line of main , so it can be considered as described above.
OS practice
On Linux, the documentation https://linux.die.net/man/2/exit says that threads terminate with _exit calling exit_group and that _exit is called after the atexit handlers. Therefore, on Linux, when calling exit all atexit handlers are started before the threads are completed. Note that they are executed in the exit stream call, not in the stream called atexit .
On Windows, the behavior will be the same if you don't care.
Templates for emergency cleaning.
Best Model: Never be in a condition requiring emergency cleaning.
- There is no guarantee that your cleanup will start because you can have
kill -9 or a power outage. - Therefore, you should be able to recover in this scenario.
- If you can restore it, you can also restore it from
abort , so you can use abort for your emergency exit.
If you cannot do this, or if you have a "cleanup" cleanup that you want to do, the atexit handlers should be fine if you first gracefully stop all threads in the process to prevent the input of inconsistent cleanups.
source share