Use MutexAutoLock for Thread::m_start_finished_mutex

This commit is contained in:
Desour 2023-06-11 22:41:14 +02:00 committed by sfan5
parent 5d863d7e9c
commit 34ad551efc

@ -99,7 +99,6 @@ Thread::~Thread()
// Make sure start finished mutex is unlocked before it's destroyed // Make sure start finished mutex is unlocked before it's destroyed
if (m_start_finished_mutex.try_lock()) if (m_start_finished_mutex.try_lock())
m_start_finished_mutex.unlock(); m_start_finished_mutex.unlock();
} }
@ -113,7 +112,8 @@ bool Thread::start()
m_request_stop = false; m_request_stop = false;
// The mutex may already be locked if the thread is being restarted // The mutex may already be locked if the thread is being restarted
m_start_finished_mutex.try_lock(); // FIXME: what if this fails, or if already locked by same thread?
MutexAutoLock sf_lock(m_start_finished_mutex, std::try_to_lock);
try { try {
m_thread_obj = new std::thread(threadProc, this); m_thread_obj = new std::thread(threadProc, this);
@ -125,7 +125,7 @@ bool Thread::start()
sleep_ms(1); sleep_ms(1);
// Allow spawned thread to continue // Allow spawned thread to continue
m_start_finished_mutex.unlock(); sf_lock.unlock();
m_joinable = true; m_joinable = true;
@ -183,7 +183,7 @@ void Thread::threadProc(Thread *thr)
// Wait for the thread that started this one to finish initializing the // Wait for the thread that started this one to finish initializing the
// thread handle so that getThreadId/getThreadHandle will work. // thread handle so that getThreadId/getThreadHandle will work.
thr->m_start_finished_mutex.lock(); MutexAutoLock sf_lock(thr->m_start_finished_mutex);
thr->m_retval = thr->run(); thr->m_retval = thr->run();
@ -191,7 +191,7 @@ void Thread::threadProc(Thread *thr)
// Unlock m_start_finished_mutex to prevent data race condition on Windows. // Unlock m_start_finished_mutex to prevent data race condition on Windows.
// On Windows with VS2017 build TerminateThread is called and this mutex is not // On Windows with VS2017 build TerminateThread is called and this mutex is not
// released. We try to unlock it from caller thread and it's refused by system. // released. We try to unlock it from caller thread and it's refused by system.
thr->m_start_finished_mutex.unlock(); sf_lock.unlock();
g_logger.deregisterThread(); g_logger.deregisterThread();
} }