diff --git a/src/thread.h b/src/thread.h index bcd4690811..24f4f8e2aa 100644 --- a/src/thread.h +++ b/src/thread.h @@ -14,6 +14,7 @@ #include "crashlog.h" #include #include +#include /** * Sleep on the current thread for a defined time. @@ -46,7 +47,17 @@ inline bool StartNewThread(std::thread *thr, const char *name, TFn&& _Fx, TArgs& { #ifndef NO_THREADS try { + static std::mutex thread_startup_mutex; + std::lock_guard lock(thread_startup_mutex); + std::thread t([] (const char *name, TFn&& F, TArgs&&... A) { + /* Delay starting the thread till the main thread is finished + * with the administration. This prevent race-conditions on + * startup. */ + { + std::lock_guard lock(thread_startup_mutex); + } + SetCurrentThreadName(name); CrashLog::InitThread(); try {