13
13
// limitations under the License.
14
14
15
15
#include < array>
16
+ #include < atomic>
16
17
#include < chrono>
17
- #include < condition_variable>
18
- #include < mutex>
19
18
#include < set>
20
19
#include < thread>
21
20
@@ -850,18 +849,21 @@ TEST(XTypesTestsThreadSafety, TypeObjectFactoryGetInstanceIsThreadSafe)
850
849
constexpr size_t num_threads = 10 ;
851
850
std::array<TypeObjectFactory*, num_threads> factories;
852
851
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 };
855
853
856
854
// Create threads that get an instance of the factory
857
855
for (size_t i = 0 ; i < num_threads; ++i)
858
856
{
859
857
threads[i] = std::thread (
860
- [&cv, &mutexes , &factories, i]()
858
+ [&n_threads_started , &factories, i]()
861
859
{
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
+ }
865
867
866
868
// Get the instance from all threads at the same time
867
869
auto factory = TypeObjectFactory::get_instance ();
@@ -870,10 +872,6 @@ TEST(XTypesTestsThreadSafety, TypeObjectFactoryGetInstanceIsThreadSafe)
870
872
});
871
873
}
872
874
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
-
877
875
// Wait for all threads to finish
878
876
for (size_t i = 0 ; i < num_threads; ++i)
879
877
{
0 commit comments