adapted tests
This commit is contained in:
@@ -13,62 +13,41 @@
|
||||
#include <future>
|
||||
#include <boost/heap/priority_queue.hpp>
|
||||
|
||||
#include "IBlockingQueue.h"
|
||||
|
||||
using namespace boost;
|
||||
|
||||
template <typename T>
|
||||
class BlockingQueue
|
||||
class BlockingQueue : public IBlockingQueue<T>
|
||||
{
|
||||
private:
|
||||
std::mutex mutex;
|
||||
heap::priority_queue<T, heap::stable<true>> p_queue;
|
||||
std::mutex mutex;
|
||||
std::condition_variable wait_condition;
|
||||
|
||||
std::promise<T> promise;
|
||||
std::shared_future<T> future_pop;
|
||||
heap::priority_queue<T, heap::stable<true>> p_queue;
|
||||
|
||||
public:
|
||||
BlockingQueue()
|
||||
{
|
||||
this->future_pop = promise.get_future();
|
||||
}
|
||||
|
||||
void push(const T& value)
|
||||
{
|
||||
std::lock_guard lock(this->mutex);
|
||||
if(this->p_queue.empty())
|
||||
void push(T const& value)
|
||||
{
|
||||
this->promise.set_value(value);
|
||||
return;
|
||||
}
|
||||
p_queue.push(value);
|
||||
}
|
||||
std::lock_guard lock(this->mutex);
|
||||
|
||||
T pop()
|
||||
{
|
||||
std::unique_lock lock(this->mutex);
|
||||
p_queue.push(value);
|
||||
|
||||
auto status = future_pop.wait_for(std::chrono::seconds(0));
|
||||
if(status == std::future_status::ready)
|
||||
{
|
||||
return future_pop.get();
|
||||
wait_condition.notify_one();
|
||||
}
|
||||
|
||||
if (not this->p_queue.empty())
|
||||
T pop()
|
||||
{
|
||||
std::unique_lock lock(this->mutex);
|
||||
while(this->p_queue.empty())
|
||||
{
|
||||
wait_condition.wait(lock);
|
||||
}
|
||||
|
||||
auto element = p_queue.top();
|
||||
p_queue.pop();
|
||||
return element;
|
||||
}
|
||||
|
||||
lock.unlock();
|
||||
|
||||
return future_pop.get();
|
||||
}
|
||||
|
||||
private:
|
||||
T get_first()
|
||||
{
|
||||
std::unique_lock lock(this->mutex);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user