From cf0a3209b51babf72469d962914db0dac2e5f52c Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期二, 27 十二月 2022 14:13:30 +0800
Subject: [PATCH] add get msg timeout

---
 fixed_q.h |   30 +++++++++++++++++++++++++++---
 1 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/fixed_q.h b/fixed_q.h
index 2e4fe94..4620d48 100644
--- a/fixed_q.h
+++ b/fixed_q.h
@@ -46,6 +46,17 @@
         cond_node.notify();
         return q.size();
     }
+    int pop(T& m, const size_t ms){
+        std::unique_lock<std::mutex> l{mtx_q};
+        auto d = std::chrono::milliseconds{ms};
+        if(!cond_node.wait_for(l, d, [this]{ return !q.empty(); })){
+            return -1;
+        }
+        m = q.front();
+        q.pop_front();
+        cond_spare.notify();
+        return q.size();
+    }
     int pop(T& m){
         std::unique_lock<std::mutex> l{mtx_q};
         auto d = std::chrono::milliseconds{du};
@@ -57,16 +68,29 @@
         cond_spare.notify();
         return q.size();
     }
+    T pop(const size_t ms){
+        std::unique_lock<std::mutex> l{mtx_q};
+        auto d = std::chrono::milliseconds{ms};
+        T t{};
+        if(!cond_node.wait_for(l, d, [this]{ return !q.empty(); })){
+            return t;
+        }
+        t = q.front();
+        q.pop_front();
+        cond_spare.notify();
+        return t;
+    }
     T pop(){
         std::unique_lock<std::mutex> l{mtx_q};
         auto d = std::chrono::milliseconds{du};
+        T t{};
         while(!cond_node.wait_for(l, d, [this]{ return !q.empty(); })){
-            if (pred()) return T{};
+            if (pred()) return t;
         }
-        auto m(move(q.front()));
+        t = q.front();
         q.pop_front();
         cond_spare.notify();
-        return m;
+        return t;
     }
 
     template<typename F> void clear(F&& f){

--
Gitblit v1.8.0