From dd8935b07d29595cefd82ef5d49fda4185dde4e7 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 12 Mar 2023 18:44:01 +0100 Subject: [PATCH] Fix waiting for threads to exit in wxCondition unit test Using sleep was not only fragile but also resulted in tons of TSAN errors, so replace it by the similar approach to the one which was already used to wait for the threads to start up. This is horribly inefficient but we don't care about this in the test and, at least, this does ensure that the threads exit before it ends. --- tests/thread/misc.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/thread/misc.cpp b/tests/thread/misc.cpp index 4915e928fa..17b7d3b82a 100644 --- a/tests/thread/misc.cpp +++ b/tests/thread/misc.cpp @@ -143,6 +143,7 @@ public: m_mutex->Unlock(); //wxPrintf(wxT("Thread %lu finished to wait, exiting.\n"), GetId()); + gs_cond.Post(); return nullptr; } @@ -389,29 +390,29 @@ void MiscThreadTestCase::TestThreadConditions() } // wait until all threads run - // NOTE: main thread is waiting for the other threads to start size_t nRunning = 0; while ( nRunning < WXSIZEOF(threads) ) { CPPUNIT_ASSERT_EQUAL( wxSEMA_NO_ERROR, gs_cond.Wait() ); nRunning++; - - // note that main thread is already running } wxMilliSleep(500); -#if 1 // now wake one of them up CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR, condition.Signal() ); -#endif - wxMilliSleep(200); + CPPUNIT_ASSERT_EQUAL( wxSEMA_NO_ERROR, gs_cond.Wait() ); + size_t nFinished = 1; // wake all the (remaining) threads up, so that they can exit CPPUNIT_ASSERT_EQUAL( wxCOND_NO_ERROR, condition.Broadcast() ); - // give them time to terminate (dirty!) - wxMilliSleep(500); + while ( nFinished < WXSIZEOF(threads) ) + { + CPPUNIT_ASSERT_EQUAL( wxSEMA_NO_ERROR, gs_cond.Wait() ); + + nFinished++; + } }