abortable pop

This commit is contained in:
badaix 2016-11-09 19:26:10 +01:00
parent 935899b0a2
commit d0c88e2ce0

View file

@ -52,9 +52,7 @@ public:
bool try_pop(T& item, std::chrono::microseconds timeout) bool try_pop(T& item, std::chrono::microseconds timeout)
{ {
std::unique_lock<std::mutex> mlock(mutex_); std::unique_lock<std::mutex> mlock(mutex_);
cond_.wait_for(mlock, timeout);
if (!cond_.wait_for(mlock, timeout, [this] { return !queue_.empty(); }))
return false;
if (queue_.empty()) if (queue_.empty())
return false; return false;
@ -75,15 +73,17 @@ public:
cond_.notify_one(); cond_.notify_one();
} }
void pop(T& item) bool pop(T& item)
{ {
std::unique_lock<std::mutex> mlock(mutex_); std::unique_lock<std::mutex> mlock(mutex_);
while (queue_.empty()) cond_.wait(mlock);
{
cond_.wait(mlock); if (queue_.empty())
} return false;
item = queue_.front();
item = std::move(queue_.front());
queue_.pop(); queue_.pop();
return true;
} }
void push(const T& item) void push(const T& item)