[libcxx-commits] [libcxx] b3b611f - [libc++] Fix flakiness in `atomic_notify_all.pass.cpp` (#70436)

via libcxx-commits libcxx-commits at lists.llvm.org
Fri Oct 27 06:49:58 PDT 2023


Author: Konstantin Varlamov
Date: 2023-10-27T09:49:54-04:00
New Revision: b3b611fe1a2f41e3e3ccf294c3fd9b1316e70c4d

URL: https://github.com/llvm/llvm-project/commit/b3b611fe1a2f41e3e3ccf294c3fd9b1316e70c4d
DIFF: https://github.com/llvm/llvm-project/commit/b3b611fe1a2f41e3e3ccf294c3fd9b1316e70c4d.diff

LOG: [libc++] Fix flakiness in `atomic_notify_all.pass.cpp` (#70436)

Avoid relying on sleep for synchronization.

Added: 
    

Modified: 
    libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.wait/atomic_notify_all.pass.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.wait/atomic_notify_all.pass.cpp b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.wait/atomic_notify_all.pass.cpp
index d22ea426057113b..2c8161eedbc4dec 100644
--- a/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.wait/atomic_notify_all.pass.cpp
+++ b/libcxx/test/std/atomics/atomics.types.operations/atomics.types.operations.wait/atomic_notify_all.pass.cpp
@@ -56,15 +56,21 @@ struct TestFn {
     {
       volatile A a(T(2));
       static_assert(noexcept(std::atomic_notify_all(&a)), "");
-      auto f = [&]() {
+
+      std::atomic<bool> is_ready[2] = {false, false};
+      auto f                        = [&](int index) {
         assert(std::atomic_load(&a) == T(2));
+        is_ready[index].store(true);
+
         std::atomic_wait(&a, T(2));
         assert(std::atomic_load(&a) == T(4));
       };
-      std::thread t1 = support::make_test_thread(f);
-      std::thread t2 = support::make_test_thread(f);
-      std::this_thread::sleep_for(std::chrono::milliseconds(100));
+      std::thread t1 = support::make_test_thread(f, /*index=*/0);
+      std::thread t2 = support::make_test_thread(f, /*index=*/1);
 
+      while (!is_ready[0] || !is_ready[1]) {
+        // Spin
+      }
       std::atomic_store(&a, T(4));
       std::atomic_notify_all(&a);
       t1.join();


        


More information about the libcxx-commits mailing list