made BlockingQueue stable
This commit is contained in:
@@ -10,8 +10,9 @@
|
||||
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <queue>
|
||||
#include <boost/heap/priority_queue.hpp>
|
||||
|
||||
using namespace boost;
|
||||
|
||||
template <typename T>
|
||||
class BlockingQueue
|
||||
@@ -19,21 +20,22 @@ class BlockingQueue
|
||||
private:
|
||||
std::mutex d_mutex;
|
||||
std::condition_variable d_condition;
|
||||
std::priority_queue<T> d_queue;
|
||||
heap::priority_queue<T, heap::stable<T>> p_queue;
|
||||
|
||||
public:
|
||||
void push(T const& value) {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(this->d_mutex);
|
||||
d_queue.push_front(value);
|
||||
p_queue.push_front(value);
|
||||
}
|
||||
this->d_condition.notify_one();
|
||||
}
|
||||
|
||||
T pop() {
|
||||
std::unique_lock<std::mutex> lock(this->d_mutex);
|
||||
this->d_condition.wait(lock, [=]{ return !this->d_queue.empty(); });
|
||||
T rc(std::move(this->d_queue.back()));
|
||||
this->d_queue.pop_back();
|
||||
this->d_condition.wait(lock, [=]{ return !this->p_queue.empty(); });
|
||||
T rc(std::move(this->p_queue.back()));
|
||||
this->p_queue.pop_back();
|
||||
return rc;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user