fixed blocking queue again
This commit is contained in:
@@ -10,7 +10,6 @@
|
|||||||
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <future>
|
|
||||||
#include <boost/heap/priority_queue.hpp>
|
#include <boost/heap/priority_queue.hpp>
|
||||||
|
|
||||||
using namespace boost;
|
using namespace boost;
|
||||||
@@ -19,73 +18,27 @@ template <typename T>
|
|||||||
class BlockingQueue
|
class BlockingQueue
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::mutex mutex;
|
std::mutex d_mutex;
|
||||||
heap::priority_queue<T, heap::stable<true>> p_queue;
|
std::condition_variable d_condition;
|
||||||
|
heap::priority_queue<T, heap::stable<true>> p_queue;
|
||||||
std::promise<T> promise;
|
|
||||||
std::shared_future<T> future_pop;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BlockingQueue()
|
void push(T const& value)
|
||||||
{
|
|
||||||
this->future_pop = promise.get_future();
|
|
||||||
}
|
|
||||||
|
|
||||||
void push(const T& value)
|
|
||||||
{
|
|
||||||
std::lock_guard lock(this->mutex);
|
|
||||||
if(this->p_queue.empty())
|
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(this->d_mutex);
|
std::unique_lock<std::mutex> lock(this->d_mutex);
|
||||||
p_queue.push(value);
|
p_queue.push(value);
|
||||||
}
|
}
|
||||||
this->d_condition.notify_one();
|
this->d_condition.notify_one();
|
||||||
=======
|
|
||||||
this->promise.set_value(value);
|
|
||||||
return;
|
|
||||||
>>>>>>> 3e086976076bbe7c104001de6366eff270d039fb
|
|
||||||
}
|
}
|
||||||
p_queue.push(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
T pop()
|
T pop()
|
||||||
{
|
|
||||||
std::unique_lock lock(this->mutex);
|
|
||||||
|
|
||||||
auto status = future_pop.wait_for(std::chrono::seconds(0));
|
|
||||||
if(status == std::future_status::ready)
|
|
||||||
{
|
{
|
||||||
<<<<<<< HEAD
|
|
||||||
// TODO die queue funzt net weil wir nich pushen koennen wenn wir im pop warten
|
|
||||||
// TODO denk ma ueber future nach
|
|
||||||
std::unique_lock<std::mutex> lock(this->d_mutex);
|
std::unique_lock<std::mutex> lock(this->d_mutex);
|
||||||
this->d_condition.wait(lock, [=]{ return !this->p_queue.empty(); });
|
this->d_condition.wait(lock, [=]{ return !this->p_queue.empty(); });
|
||||||
T rc = *this->p_queue.begin();
|
T rc = *this->p_queue.begin();
|
||||||
this->p_queue.pop();
|
this->p_queue.pop();
|
||||||
return rc;
|
return rc;
|
||||||
=======
|
|
||||||
return future_pop.get();
|
|
||||||
>>>>>>> 3e086976076bbe7c104001de6366eff270d039fb
|
|
||||||
}
|
|
||||||
|
|
||||||
if (not this->p_queue.empty())
|
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ SCENARIO("An EventHandler gets [un]registered at the notifier", "[un-register no
|
|||||||
|
|
||||||
THEN("The EventHandler gets saved")
|
THEN("The EventHandler gets saved")
|
||||||
{
|
{
|
||||||
REQUIRE(*(notifier.event_handler.find(&event_handler_mock.get())) == &event_handler_mock.get());
|
REQUIRE(*notifier.event_handler.begin() == &event_handler_mock.get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user