Skip to content

Commit 3e59261

Browse files
Refs #21664. Avoid using condition_variable in the test.
Signed-off-by: Miguel Company <[email protected]>
1 parent 0cb9e2a commit 3e59261

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

test/unittest/xtypes/XTypesTests.cpp

+10-12
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
// limitations under the License.
1414

1515
#include <array>
16+
#include <atomic>
1617
#include <chrono>
17-
#include <condition_variable>
18-
#include <mutex>
1918
#include <set>
2019
#include <thread>
2120

@@ -850,18 +849,21 @@ TEST(XTypesTestsThreadSafety, TypeObjectFactoryGetInstanceIsThreadSafe)
850849
constexpr size_t num_threads = 10;
851850
std::array<TypeObjectFactory*, num_threads> factories;
852851
std::array<std::thread, num_threads> threads;
853-
std::array<std::mutex, num_threads> mutexes;
854-
std::condition_variable cv;
852+
std::atomic<size_t> n_threads_started{0};
855853

856854
// Create threads that get an instance of the factory
857855
for (size_t i = 0; i < num_threads; ++i)
858856
{
859857
threads[i] = std::thread(
860-
[&cv, &mutexes, &factories, i]()
858+
[&n_threads_started, &factories, i]()
861859
{
862-
// Wait for main thread to notify that all threads are ready
863-
std::unique_lock<std::mutex> lock(mutexes[i]);
864-
cv.wait(lock);
860+
// Indicate this thread started
861+
++n_threads_started;
862+
// Wait for all threads to be ready
863+
while (num_threads > n_threads_started)
864+
{
865+
std::this_thread::sleep_for(std::chrono::milliseconds(1);
866+
}
865867

866868
// Get the instance from all threads at the same time
867869
auto factory = TypeObjectFactory::get_instance();
@@ -870,10 +872,6 @@ TEST(XTypesTestsThreadSafety, TypeObjectFactoryGetInstanceIsThreadSafe)
870872
});
871873
}
872874

873-
// Give time for all threads to start and notify that they are ready
874-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
875-
cv.notify_all();
876-
877875
// Wait for all threads to finish
878876
for (size_t i = 0; i < num_threads; ++i)
879877
{

0 commit comments

Comments
 (0)