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) {