[libcxx-commits] [libcxx] [libc++] Fix wait_on_destruct.pass.cpp hanging sometimes (PR #146240)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Jun 30 06:02:37 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));
----------------
EricWF wrote:

Sure, fair enough. The sleep here just enhances the likelyhood that the main thread will have exited main by the time `value` is touched. But I'm happy to remove it.



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


More information about the libcxx-commits mailing list