[libcxx-commits] [libcxx] [libc++] Fix another potentially flaky atomic test. (PR #71011)

Konstantin Varlamov via libcxx-commits libcxx-commits at lists.llvm.org
Wed Nov 1 19:42:33 PDT 2023


https://github.com/var-const created https://github.com/llvm/llvm-project/pull/71011

This is a follow-up to https://github.com/llvm/llvm-project/pull/70436.


>From 4c0b0d5553223b03a689bee74e44cc4d8bf52ac0 Mon Sep 17 00:00:00 2001
From: Konstantin Varlamov <varconsteq at gmail.com>
Date: Wed, 1 Nov 2023 19:41:13 -0700
Subject: [PATCH] [libc++] Fix another potentially flaky atomic test.

This is a follow-up to https://github.com/llvm/llvm-project/pull/70436.
---
 .../atomic_notify_all.pass.cpp                     | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

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 2c8161eedbc4dec..e446bcf5cfc976b 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
@@ -39,15 +39,21 @@ struct TestFn {
     {
       A a(T(1));
       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(1));
+        is_ready[index].store(true);
+
         std::atomic_wait(&a, T(1));
         assert(std::atomic_load(&a) == T(3));
       };
-      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(3));
       std::atomic_notify_all(&a);
       t1.join();



More information about the libcxx-commits mailing list