From 793eaa4c711b42a4c2f5f543aff2abeeeb3a4349 Mon Sep 17 00:00:00 2001 From: Roker Date: Thu, 25 Oct 2018 17:39:54 +0200 Subject: [PATCH] add locked_queue<>.try_pop_front() with timeout in seconds, instead of absolute time. --- locked_queue.hh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/locked_queue.hh b/locked_queue.hh index 632c004..a84cfdb 100644 --- a/locked_queue.hh +++ b/locked_queue.hh @@ -97,6 +97,22 @@ namespace utility return true; } + // returns true and set a copy of the first element and pop it from queue if there is any + // returns false and leaves 'out' untouched if queue is empty even after 'duration' + bool try_pop_front(T& out, std::chrono::seconds duration) + { + Lock L(_mtx); + if(! _cv.wait_for(L, duration, [this]{ return !_q.empty(); } ) ) + { + return false; + } + + out = std::move(_q.front()); + _q.pop_front(); + return true; + } + + void push_back(const T& data) { {