From 4aaab693148e3f98923f8f77ff02c665e9d25d10 Mon Sep 17 00:00:00 2001 From: Eric Date: Mon, 30 Jun 2025 11:54:12 -0400 Subject: [PATCH] [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. --- .../futures.async/wait_on_destruct.pass.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/libcxx/test/std/thread/futures/futures.async/wait_on_destruct.pass.cpp b/libcxx/test/std/thread/futures/futures.async/wait_on_destruct.pass.cpp index 8260ec3dfcaf..d2964e02257d 100644 --- a/libcxx/test/std/thread/futures/futures.async/wait_on_destruct.pass.cpp +++ b/libcxx/test/std/thread/futures/futures.async/wait_on_destruct.pass.cpp @@ -20,22 +20,24 @@ #include #include #include +#include +#include + std::mutex mux; int main(int, char**) { - using namespace std::chrono_literals; + std::condition_variable cond; std::unique_lock lock(mux); - std::atomic in_async = false; - auto v = std::async(std::launch::async, [&in_async, value = 1]() mutable { - in_async = true; - in_async.notify_all(); - std::scoped_lock thread_lock(mux); + auto v = std::async(std::launch::async, [&cond, value = 1]() mutable { + std::unique_lock thread_lock(mux); + cond.notify_all(); + thread_lock.unlock(); + value = 4; (void)value; }); - in_async.wait(true); - lock.unlock(); + cond.wait(lock); return 0; }