From 95c6e6652c4fd1ca0fb1154733e0586eee7fe397 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 | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/test/unittest/xtypes/XTypesTests.cpp b/test/unittest/xtypes/XTypesTests.cpp index ddbaeb8b938..cc811741b04 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 @@ -847,21 +846,24 @@ TEST_F(XTypesTests, MemberDescriptorFullyQualifiedName) */ TEST(XTypesTestsThreadSafety, TypeObjectFactoryGetInstanceIsThreadSafe) { - constexpr size_t num_threads = 10; + const 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]() + [num_threads, &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) {