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

David Spickett via lldb-commits lldb-commits at lists.llvm.org
Mon Sep 28 06:20:32 PDT 2026


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

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.

>From e0e91ff2015c7d17f6342074c5a29ba1c47f97ed Mon Sep 17 00:00:00 2001
From: David Spickett <david.spickett at arm.com>
Date: Mon, 28 Sep 2026 13:11:57 +0000
Subject: [PATCH] [lldb][test] Simplify multithreaded_queue

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.
---
 lldb/test/API/api/multithreaded/common.h | 32 +++++++++++-------------
 1 file changed, 14 insertions(+), 18 deletions(-)

diff --git a/lldb/test/API/api/multithreaded/common.h b/lldb/test/API/api/multithreaded/common.h
index 0df4b771f95500..64faa675712578 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;
   }
 };
 



More information about the lldb-commits mailing list