Files
flippr-code/FlippR-Driver/src/utilities/BlockingQueue.hpp
2018-06-07 20:23:12 +02:00

46 lines
993 B
C++

/*
* BlockingQueue.hpp
*
* Created on: May 17, 2018
* Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert, Rafael Vinci, Dr. Franca Rupprecht
*/
#ifndef SRC_UTILITIES_BLOCKINGQUEUE_HPP_
#define SRC_UTILITIES_BLOCKINGQUEUE_HPP_
#include <mutex>
#include <condition_variable>
#include <boost/heap/priority_queue.hpp>
using namespace boost;
template <typename T>
class BlockingQueue
{
private:
std::mutex d_mutex;
std::condition_variable d_condition;
heap::priority_queue<T, heap::stable<true>> p_queue;
public:
void push(T const& value)
{
std::unique_lock<std::mutex> lock(this->d_mutex);
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->p_queue.empty(); });
T rc(std::move(this->p_queue.back()));
this->p_queue.pop_back();
return rc;
}
};
#endif /* SRC_UTILITIES_BLOCKINGQUEUE_HPP_ */