Skip to content

Commit 4aaab69

Browse files
authored
[libc++] Fix wait_on_destruct.pass.cpp hanging sometimes (#146240)
This test was deadlocking on my machine. It seems to me the intention of `in_async.wait(...)` was to wait for the value to be set to true, which requires a call of `wait(false)` (waits if value matches argument). ~As a drive by change scoped_lock to unique_lock, since there shouldn't be any functional difference between the two in this test.~ I've addressed the issues with the `in_async` by switching to a condition variable instead, since my first attempt at fixing this with `in_async` wasn't sufficient.
1 parent 199c6ec commit 4aaab69

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

libcxx/test/std/thread/futures/futures.async/wait_on_destruct.pass.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,24 @@
2020
#include <atomic>
2121
#include <future>
2222
#include <mutex>
23+
#include <condition_variable>
24+
#include <thread>
25+
2326

2427
std::mutex mux;
2528

2629
int main(int, char**) {
27-
using namespace std::chrono_literals;
30+
std::condition_variable cond;
2831
std::unique_lock lock(mux);
29-
std::atomic<bool> in_async = false;
30-
auto v = std::async(std::launch::async, [&in_async, value = 1]() mutable {
31-
in_async = true;
32-
in_async.notify_all();
33-
std::scoped_lock thread_lock(mux);
32+
auto v = std::async(std::launch::async, [&cond, value = 1]() mutable {
33+
std::unique_lock thread_lock(mux);
34+
cond.notify_all();
35+
thread_lock.unlock();
36+
3437
value = 4;
3538
(void)value;
3639
});
37-
in_async.wait(true);
38-
lock.unlock();
40+
cond.wait(lock);
3941

4042
return 0;
4143
}

0 commit comments

Comments
 (0)