[Lldb-commits] [lldb] [lldb][test] Simplify multithreaded_queue (PR #226979)

via lldb-commits lldb-commits at lists.llvm.org
Mon Sep 28 06:21:25 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: David Spickett (DavidSpickett)

<details>
<summary>Changes</summary>

This makes it easier to see how the timeout is applied in pop.

push now releases the mutex before notifying, where before the watchers would have to wait for push to return. Likely not going to make anything much faster, but it makes a bit more sense. Finish adding the new item, then notify.

---
Full diff: https://github.com/llvm/llvm-project/pull/226979.diff


1 Files Affected:

- (modified) lldb/test/API/api/multithreaded/common.h (+14-18) 


``````````diff
diff --git a/lldb/test/API/api/multithreaded/common.h b/lldb/test/API/api/multithreaded/common.h
index 0df4b771f9550..64faa67571257 100644
--- a/lldb/test/API/api/multithreaded/common.h
+++ b/lldb/test/API/api/multithreaded/common.h
@@ -32,33 +32,29 @@ class multithreaded_queue {
   std::condition_variable m_condition;
   std::mutex m_mutex;
   std::queue<T> m_data;
-  bool m_notified;
 
 public:
 
   void push(T e) {
-    std::lock_guard<std::mutex> lock(m_mutex);
-    m_data.push(e);
-    m_notified = true;
+    {
+      std::lock_guard<std::mutex> lock(m_mutex);
+      m_data.push(e);
+    }
     m_condition.notify_all();
   }
 
   T pop(int timeout_seconds, bool &success) {
-    int count = 0;
-    while (count < timeout_seconds) {
-      std::unique_lock<std::mutex> lock(m_mutex);
-      if (!m_data.empty()) {
-        m_notified = false;
-        T ret = m_data.front();
-        m_data.pop();
-        success = true;
-        return ret;
-      } else if (!m_notified)
-        m_condition.wait_for(lock, std::chrono::seconds(1));
-      count ++;
+    std::unique_lock<std::mutex> lock(m_mutex);
+    if (!m_condition.wait_for(lock, std::chrono::seconds(timeout_seconds),
+                              [&] { return !m_data.empty(); })) {
+      success = false;
+      return T();
     }
-    success = false;
-    return T();
+
+    T ret = m_data.front();
+    m_data.pop();
+    success = true;
+    return ret;
   }
 };
 

``````````

</details>


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


More information about the lldb-commits mailing list