summaryrefslogtreecommitdiff
path: root/src/mbgl/util/channel.hpp
blob: a48156f120f3282624555d884fdf0d485a1c9843 (plain)
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
#ifndef MBGL_UTIL_CHANNEL
#define MBGL_UTIL_CHANNEL

#include <mbgl/util/noncopyable.hpp>

#include <mutex>
#include <condition_variable>
#include <queue>

namespace mbgl {

template <class T>
class Channel : public mbgl::util::noncopyable {
public:
    void send(const T& t) {
        std::unique_lock<std::mutex> lock(mutex);
        queue.push(t);
        condition.notify_one();
    }

    T receive() {
        std::unique_lock<std::mutex> lock(mutex);
        condition.wait(lock, [&](){ return !queue.empty(); });

        T t = queue.front();
        queue.pop();

        return t;
    }

private:
    std::mutex mutex;
    std::condition_variable condition;
    std::queue<T> queue;
};

}

#endif