Skip to content

Commit b3b611f

Browse files
authored
[libc++] Fix flakiness in atomic_notify_all.pass.cpp (#70436)
Avoid relying on sleep for synchronization.
1 parent a0eb6b8 commit b3b611f

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,21 @@ struct TestFn {
5656
{
5757
volatile A a(T(2));
5858
static_assert(noexcept(std::atomic_notify_all(&a)), "");
59-
auto f = [&]() {
59+
60+
std::atomic<bool> is_ready[2] = {false, false};
61+
auto f = [&](int index) {
6062
assert(std::atomic_load(&a) == T(2));
63+
is_ready[index].store(true);
64+
6165
std::atomic_wait(&a, T(2));
6266
assert(std::atomic_load(&a) == T(4));
6367
};
64-
std::thread t1 = support::make_test_thread(f);
65-
std::thread t2 = support::make_test_thread(f);
66-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
68+
std::thread t1 = support::make_test_thread(f, /*index=*/0);
69+
std::thread t2 = support::make_test_thread(f, /*index=*/1);
6770

71+
while (!is_ready[0] || !is_ready[1]) {
72+
// Spin
73+
}
6874
std::atomic_store(&a, T(4));
6975
std::atomic_notify_all(&a);
7076
t1.join();

0 commit comments

Comments
 (0)