Skip to content

Commit 840e857

Browse files
authored
[libc++] Fix sporadic test failure in condition_variable notify_all test (#97622)
fixes #95944
1 parent d873630 commit 840e857

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

libcxx/test/std/thread/thread.condition/thread.condition.condvar/notify_all.pass.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
// void notify_all();
1616

17+
#include <atomic>
1718
#include <condition_variable>
1819
#include <mutex>
1920
#include <thread>
@@ -29,10 +30,13 @@ int test0 = 0;
2930
int test1 = 0;
3031
int test2 = 0;
3132

33+
std::atomic<int> ready_count(0);
34+
3235
void f1()
3336
{
3437
std::unique_lock<std::mutex> lk(mut);
3538
assert(test1 == 0);
39+
ready_count += 1;
3640
while (test1 == 0)
3741
cv.wait(lk);
3842
assert(test1 == 1);
@@ -43,6 +47,7 @@ void f2()
4347
{
4448
std::unique_lock<std::mutex> lk(mut);
4549
assert(test2 == 0);
50+
ready_count += 1;
4651
while (test2 == 0)
4752
cv.wait(lk);
4853
assert(test2 == 1);
@@ -53,7 +58,9 @@ int main(int, char**)
5358
{
5459
std::thread t1 = support::make_test_thread(f1);
5560
std::thread t2 = support::make_test_thread(f2);
56-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
61+
while (ready_count.load() != 2) {
62+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
63+
}
5764
{
5865
std::unique_lock<std::mutex>lk(mut);
5966
test1 = 1;

0 commit comments

Comments
 (0)