~ttt/async

e1d02f899cb6acf79f581a7a5f4f6ce9117cad27 — Tomasz Klak 8 years ago 3f9709c master
simplified blocking_queue
M AsynchronousExecutor/src/BlockingTaskQueueImpl.h => AsynchronousExecutor/src/BlockingTaskQueueImpl.h +1 -9
@@ 10,9 10,6 @@
class BlockingTaskQueueImpl : public BlockingTaskQueue
{
public:
    BlockingTaskQueueImpl()
        : blockingQueue(taskQueue, mutex, conditionVariable)
    {}

    void scheduleTaskToExecute(Task* task)
    {


@@ 30,13 27,8 @@ public:
    }

private:
    std::queue<Task*> taskQueue;
    std::mutex mutex;
    std::condition_variable conditionVariable;

    blocking_queue<Task*,
                   std::queue<Task*>,
                   std::mutex,
    blocking_queue<std::queue<Task*>,
                   std::condition_variable,
                   std::unique_lock<std::mutex> > blockingQueue;
};

M AsynchronousService/src/MessageQueueImpl.h => AsynchronousService/src/MessageQueueImpl.h +1 -10
@@ 11,10 11,6 @@
class MessageQueueImpl : public MessageQueue<Message>
{
public:
    MessageQueueImpl()
        : blockingQueue(taskQueue, mutex, conditionVariable)
    {}

    void put(Message message)
    {
        blockingQueue.push_and_notify_one(message);


@@ 26,13 22,8 @@ public:
    }

private:
    std::queue<Message> taskQueue;
    std::mutex mutex;
    std::condition_variable conditionVariable;

    blocking_queue<Message,
                   std::queue<Message>,
                   std::mutex,
    blocking_queue<std::queue<Message>,
                   std::condition_variable,
                   std::unique_lock<std::mutex> > blockingQueue;
};

M BlockingQueue/include/blocking_queue.h => BlockingQueue/include/blocking_queue.h +10 -21
@@ 1,23 1,15 @@
#ifndef BLOCKING_QUEUE_H
#define BLOCKING_QUEUE_H

template <typename T,
          typename Queue,
          typename Lockable,
template <typename Queue,
          typename ConditionVariable,
          typename LockGuard>
class blocking_queue
{
public:
    blocking_queue(Queue& queue,
                  Lockable& mutex,
                  ConditionVariable& conditionVariable)
        : queue(queue),
          mutex(mutex),
          conditionVariable(conditionVariable)
    {}
    using Element = typename Queue::value_type;

    void push_and_notify_one(T task)
    void push_and_notify_one(Element task)
    {
        {
            LockGuard lock(mutex);


@@ 28,23 20,20 @@ public:
        conditionVariable.notify_one();
    }

    T wait_and_pop()
    Element wait_and_pop()
    {
        LockGuard lock(mutex);

        while (queue.empty())
        {
            conditionVariable.wait(lock);
        }
        conditionVariable.wait(lock, [&]{ return !queue.empty(); });

        T task = queue.front();
        Element task = queue.front();

        queue.pop();

        return task;
    }

    void push_and_notify_all(T task)
    void push_and_notify_all(Element task)
    {
        {
            LockGuard lock(mutex);


@@ 56,9 45,9 @@ public:
    }

private:
    Queue& queue;
    Lockable& mutex;
    ConditionVariable& conditionVariable;
    Queue queue;
    typename LockGuard::mutex_type mutex;
    ConditionVariable conditionVariable;
};

#endif // BLOCKING_QUEUE_H