[libcxx-commits] [libcxx] Fix async test (PR #146240)

via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jun 28 14:07:35 PDT 2025


https://github.com/EricWF updated https://github.com/llvm/llvm-project/pull/146240

>From 06a816ea63c9338f80f9ea5a77bfc7e1c38f1143 Mon Sep 17 00:00:00 2001
From: Eric Fiselier <eric at efcs.ca>
Date: Sat, 28 Jun 2025 16:57:38 -0400
Subject: [PATCH 1/2] Fix async test

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.

Alternatively, the test could be written with a condition variable
directly.
---
 .../thread/futures/futures.async/wait_on_destruct.pass.cpp    | 4 ++--
 1 file changed, 2 insertions(+), 2 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 8260ec3dfcaf4..6ab9f1d0b1afb 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
@@ -30,11 +30,11 @@ int main(int, char**) {
   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);
+    std::unique_lock thread_lock(mux);
     value = 4;
     (void)value;
   });
-  in_async.wait(true);
+  in_async.wait(false);
   lock.unlock();
 
   return 0;

>From ca83b46049458067c11012fbbdb5f10bf751d44b Mon Sep 17 00:00:00 2001
From: Eric Fiselier <eric at efcs.ca>
Date: Sat, 28 Jun 2025 17:07:21 -0400
Subject: [PATCH 2/2] Use condition variable approach instead

---
 .../futures.async/wait_on_destruct.pass.cpp     | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 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 6ab9f1d0b1afb..9716ec11ad495 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,25 @@
 #include <atomic>
 #include <future>
 #include <mutex>
+#include <condition_variable>
+#include <thread>
+#include <chrono>
 
 std::mutex mux;
 
 int main(int, char**) {
-  using namespace std::chrono_literals;
+  std::condition_variable cond;
   std::unique_lock lock(mux);
-  std::atomic<bool> in_async = false;
-  auto v                     = std::async(std::launch::async, [&in_async, value = 1]() mutable {
-    in_async = true;
-    in_async.notify_all();
+  auto v = std::async(std::launch::async, [&cond, value = 1]() mutable {
     std::unique_lock thread_lock(mux);
+    cond.notify_all();
+    thread_lock.unlock();
+    std::this_thread::sleep_for(std::chrono::seconds(1));
+
     value = 4;
     (void)value;
   });
-  in_async.wait(false);
-  lock.unlock();
+  cond.wait(lock);
 
   return 0;
 }



More information about the libcxx-commits mailing list