JustEnoughMod
Loading...
Searching...
No Matches
Queue.hpp
1#ifndef UTIL_QUEUE_HPP
2#define UTIL_QUEUE_HPP
3
4#include <condition_variable>
5#include <mutex>
6#include <queue>
7
8namespace JEM {
9
10 template <typename T>
11 class queue {
12 public:
13 void push(T item) {
14 std::unique_lock<std::mutex> const lock(m_mutex);
15
16 m_queue.push(item);
17
18 m_cond.notify_all();
19 }
20
21 auto pop() -> T {
22 std::unique_lock<std::mutex> lock(m_mutex);
23
24 m_cond.wait(lock, [this]() { return !m_queue.empty(); });
25
26 T item = m_queue.front();
27 m_queue.pop();
28
29 return item;
30 }
31
32 auto empty() -> bool {
33 std::unique_lock<std::mutex> const lock(m_mutex);
34
35 return m_queue.empty();
36 }
37
38 private:
39 std::queue<T> m_queue;
40 std::mutex m_mutex;
41 std::condition_variable m_cond;
42 };
43} // namespace JEM
44
45#endif
Definition Queue.hpp:11