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

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jun 30 01:11:44 PDT 2025


================
@@ -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();
-    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();
+    std::this_thread::sleep_for(std::chrono::seconds(1));
----------------
philnik777 wrote:

I'm really not a huge fan of using `sleep_for` to test these kinds of things. This has caused numerous tests to be flaky in the past.

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


More information about the libcxx-commits mailing list