Skip to content

Commit

Permalink
Refs #21664. Avoid using condition_variable in the test.
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Company <[email protected]>
  • Loading branch information
MiguelCompany committed Sep 15, 2024
1 parent 0cb9e2a commit b0c9112
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions test/unittest/xtypes/XTypesTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
// limitations under the License.

#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <set>
#include <thread>

Expand Down Expand Up @@ -850,18 +849,21 @@ TEST(XTypesTestsThreadSafety, TypeObjectFactoryGetInstanceIsThreadSafe)
constexpr size_t num_threads = 10;
std::array<TypeObjectFactory*, num_threads> factories;
std::array<std::thread, num_threads> threads;
std::array<std::mutex, num_threads> mutexes;
std::condition_variable cv;
std::atomic<size_t> 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<std::mutex> 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();
Expand All @@ -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)
{
Expand Down

0 comments on commit b0c9112

Please sign in to comment.