This commit is contained in:
Jonas Zeunert
2018-07-11 16:52:02 +02:00
2 changed files with 54 additions and 14 deletions

View File

@@ -10,6 +10,7 @@
#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;
@@ -18,22 +19,45 @@ template <typename T>
class BlockingQueue class BlockingQueue
{ {
private: private:
std::mutex d_mutex; std::mutex mutex;
std::condition_variable d_condition; heap::priority_queue<T, heap::stable<true>> p_queue;
heap::priority_queue<T, heap::stable<true>> p_queue;
std::promise<T> promise;
std::shared_future<T> future_pop;
public: public:
void push(T const& value) BlockingQueue()
{
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 die queue funzt net weil wir nich pushen koennen wenn wir im pop warten
// TODO denk ma ueber future nach // TODO denk ma ueber future nach
std::unique_lock<std::mutex> lock(this->d_mutex); std::unique_lock<std::mutex> lock(this->d_mutex);
@@ -41,12 +65,28 @@ public:
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
} }
void release() if (not this->p_queue.empty())
{ {
this->d_condition.notify_one(); 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);
}
}; };

View File

@@ -6,8 +6,8 @@
*/ */
#include "../catch.hpp" #include "catch.hpp"
#include "../fakeit.hpp" #include "fakeit.hpp"
#include <map> #include <map>
#include <typeinfo> #include <typeinfo>
@@ -60,11 +60,11 @@ SCENARIO("Creating a Detector object", "")
AND_WHEN("an event can be found at gpio interface") AND_WHEN("an event can be found at gpio interface")
{ {
std::this_thread::sleep_for(std::chrono::milliseconds(50)); std::this_thread::sleep_for(std::chrono::milliseconds(50));
THEN("only the fitting event should be distributed by event notifier") THEN("after some time the only the fitting event should be distributed by event notifier")
{ {
REQUIRE((bool)Verify(Method(event_notifier_mock, distribute_event).Using(event2))); REQUIRE((bool)Verify(Method(event_notifier_mock, distribute_event).Using(event2)));
} }
} }
} }
} }