diff --git a/lib/libc/stdlib/exit.c b/lib/libc/stdlib/exit.c --- a/lib/libc/stdlib/exit.c +++ b/lib/libc/stdlib/exit.c @@ -48,6 +48,8 @@ */ int __isthreaded = 0; +static pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER; + /* * Exit, flushing stdio buffers if necessary. */ @@ -59,6 +61,10 @@ _thread_autoinit_dummy_decl = 1; + /* Make exit(3) thread-safe */ + if (__isthreaded) + _pthread_mutex_lock(&exit_mutex); + /* * We're dealing with cleaning up thread_local destructors in the case of * the process termination through main() exit.