From 646d25f48bc382b1bba1e990c640b3b8f4da59bc Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Mon, 21 Feb 2022 14:03:39 +0300 Subject: [PATCH] Set PThread::m_isRunning early, when the thread is created. This allows PThread::IsTerminated to return false as soon as the thread is created, which may be earlier than PThread::PX_ThreadStart manages to set it. This should fix issues when the code synchronously tests if the thread is terminated soon after spawning it. --- src/ptlib/unix/tlibthrd.cxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ptlib/unix/tlibthrd.cxx b/src/ptlib/unix/tlibthrd.cxx index 8c224b2..722672e 100644 --- a/src/ptlib/unix/tlibthrd.cxx +++ b/src/ptlib/unix/tlibthrd.cxx @@ -496,10 +496,9 @@ PThread::~PThread() void * PThread::PX_ThreadStart(void * arg) -{ +{ PThread * thread = (PThread *)arg; SetCurrentThread(thread); - __atomic_store_n(&thread->m_isRunning, true, __ATOMIC_RELAXED); // Added this to guarantee that the thread creation (PThread::Restart) // has completed before we start the thread. Then the m_threadId has // been set. @@ -589,6 +588,7 @@ void PThread::Restart() // create the thread PAssertPTHREAD(pthread_create, (&m_threadId, &threadAttr, PX_ThreadStart, this)); m_threadIdValid = true; + __atomic_store_n(&m_isRunning, true, __ATOMIC_RELAXED); // put the thread into the thread list process.PXSetThread(m_threadId, this);