From b0c911243ffad8e36259650cd633c78ad3272729 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Sat, 14 Sep 2024 21:38:14 +0200 Subject: [PATCH] Refs #21664. Avoid using condition_variable in the test. Signed-off-by: Miguel Company --- test/unittest/xtypes/XTypesTests.cpp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/test/unittest/xtypes/XTypesTests.cpp b/test/unittest/xtypes/XTypesTests.cpp index ddbaeb8b938..a0fb4e49aeb 100644 --- a/test/unittest/xtypes/XTypesTests.cpp +++ b/test/unittest/xtypes/XTypesTests.cpp @@ -13,9 +13,8 @@ // limitations under the License. #include +#include #include -#include -#include #include #include @@ -850,18 +849,21 @@ TEST(XTypesTestsThreadSafety, TypeObjectFactoryGetInstanceIsThreadSafe) constexpr size_t num_threads = 10; std::array factories; std::array threads; - std::array mutexes; - std::condition_variable cv; + std::atomic n_threads_started{0}; // Create threads that get an instance of the factory for (size_t i = 0; i < num_threads; ++i) { threads[i] = std::thread( - [&cv, &mutexes, &factories, i]() + [&n_threads_started, &factories, i]() { - // Wait for main thread to notify that all threads are ready - std::unique_lock lock(mutexes[i]); - cv.wait(lock); + // Indicate this thread started + ++n_threads_started; + // Wait for all threads to be ready + while (num_threads > n_threads_started) + { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } // Get the instance from all threads at the same time auto factory = TypeObjectFactory::get_instance(); @@ -870,10 +872,6 @@ TEST(XTypesTestsThreadSafety, TypeObjectFactoryGetInstanceIsThreadSafe) }); } - // Give time for all threads to start and notify that they are ready - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - cv.notify_all(); - // Wait for all threads to finish for (size_t i = 0; i < num_threads; ++i) {