From fffd079f3c01aa175004fb9783eabaf9cbb87325 Mon Sep 17 00:00:00 2001 From: Benn Snyder Date: Sat, 31 Jan 2015 23:32:29 -0500 Subject: [PATCH 1/2] Add Queue::tryPop Now consumers don't have to block forever. --- Concurrency/Queue/Queue.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Concurrency/Queue/Queue.h b/Concurrency/Queue/Queue.h index 854851e..a719405 100644 --- a/Concurrency/Queue/Queue.h +++ b/Concurrency/Queue/Queue.h @@ -39,6 +39,19 @@ class Queue item = queue_.front(); queue_.pop(); } + + template + bool tryPop(T& item, const Timespan& timeout) + { + std::unique_lock mlock(mutex_); + if (queue_.empty() && cond_.wait_for(mlock, timeout) == std::cv_status::timeout) + { + return false; + } + item = queue_.front(); + queue_.pop(); + return true; + } void push(const T& item) { From dcdc0df868d013383a9421b5223d34308626ddb3 Mon Sep 17 00:00:00 2001 From: Benn Snyder Date: Mon, 2 Feb 2015 12:03:24 -0500 Subject: [PATCH 2/2] Queue::tryPop uses more specific type --- Concurrency/Queue/Queue.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Concurrency/Queue/Queue.h b/Concurrency/Queue/Queue.h index a719405..933e2fc 100644 --- a/Concurrency/Queue/Queue.h +++ b/Concurrency/Queue/Queue.h @@ -40,8 +40,8 @@ class Queue queue_.pop(); } - template - bool tryPop(T& item, const Timespan& timeout) + template + bool tryPop(T& item, const std::chrono::duration& timeout) { std::unique_lock mlock(mutex_); if (queue_.empty() && cond_.wait_for(mlock, timeout) == std::cv_status::timeout)