~ttt/async

ref: 3f9709cd0f81e1a7025effd4125a781b2a4048b9 async/AsynchronousService/src/MessageQueueImpl.h -rw-r--r-- 877 bytes
3f9709cd — Tomasz Kłak Merged mwaga/async into master 8 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef MESSAGEQUEUEIMPL_H
#define MESSAGEQUEUEIMPL_H

#include <queue>
#include <mutex>
#include <condition_variable>
#include "Message.h"
#include "MessageQueue.h"
#include "blocking_queue.h"

class MessageQueueImpl : public MessageQueue<Message>
{
public:
    MessageQueueImpl()
        : blockingQueue(taskQueue, mutex, conditionVariable)
    {}

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

    Message get()
    {
        return blockingQueue.wait_and_pop();
    }

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

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

#endif // MESSAGEQUEUEIMPL_H