Issue 1010: Termination of the execution with threads

Authors: Jens Gustedt
Date: 2025-03-13
Submitted against: C23
Status: Open

In C23 there is still ambiguity concerning the termination of the execution in the presence of threads, in particular if some threads are detached and not joined during the execution. Here, the behavior of thrd_exit for the last thread is not sufficiently clarified. C23 7.28.5.5 (The thrd_exit function) p5 states

The program terminates normally after the last thread has been terminated. The behavior is as if the program called the exit function with the status EXIT_SUCCESS at thread termination time.

This text seems to indicate that the notion of happens-before extends to the overall termination of all threads (thus a "last" thread), but it leaves it unspecified whether or not the execution of the cleanup code that is run on exit (atexit handlers, closure of streams and similar) synchronizes with threads that have been detached.

Our observation of implementations seems to indicate that such a synchronization does indeed take place, it would be better to note this explicitly in the standard. We propose to change the above paragraph to the following:

The program terminates normally after the last thread has been terminated. The behavior is as if the program end of the call to thrd_exit of this last thread synchronized with all terminations of other threads of the execution and thereafter is called the exit function with the status EXIT_SUCCESS at thread termination time.


Comment from Issues list maintainer on 2025-06-27:

Reflector message 29707 provides a revised wording suggestion and reflector message 29718 comments on that suggestion.