You could use boost::lockfree library to achieve desired behavior.
In that case you can implement Take
and TryTake
on top of pop method.
Or you can use this answer to protect std::deque
with std::mutex
and std::condition_variable
(answer written in terms of C++11 but you could use boost::thread to access thread related stuff from older compilers).
UPDATE
Actually I don't recommend first way cause it kills whole idea of lock-free container :)
So, for the second case TryTake (tryPop)
can be implemented with following code (just example)
template<typename PeriodT>
bool tryPop (T & v, PeriodT dur) {
std::unique_lock<std::mutex> lock(this->d_mutex);
if (!this->d_condition.wait_for(lock, dur, [=]{ return !this->d_queue.empty(); })) {
return false;
}
v = std::move (this->d_queue.back());
this->d_queue.pop_back();
return true;
}
PeriodT
can be std::chrono::milliseconds
, for example. Quick sample:
queue<int> p;
int v;
p.tryPop (v, std::chrono::milliseconds(1));