Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc++] Fix sporadic test failure in condition_variable notify_all test #97622

Merged
merged 3 commits into from
Jul 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

// void notify_all();

#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>
Expand All @@ -29,10 +30,13 @@ int test0 = 0;
int test1 = 0;
int test2 = 0;

std::atomic<int> ready_count = 0;

void f1()
{
std::unique_lock<std::mutex> lk(mut);
assert(test1 == 0);
ready_count.fetch_add(1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slight preference for ready_count += 1 since that's easier to read.

while (test1 == 0)
cv.wait(lk);
assert(test1 == 1);
Expand All @@ -43,6 +47,7 @@ void f2()
{
std::unique_lock<std::mutex> lk(mut);
assert(test2 == 0);
ready_count.fetch_add(1);
while (test2 == 0)
cv.wait(lk);
assert(test2 == 1);
Expand All @@ -53,7 +58,9 @@ int main(int, char**)
{
std::thread t1 = support::make_test_thread(f1);
std::thread t2 = support::make_test_thread(f2);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
while (ready_count.load() != 2) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
{
std::unique_lock<std::mutex>lk(mut);
test1 = 1;
Expand Down
Loading