-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_queue.h
64 lines (52 loc) · 1.63 KB
/
task_queue.h
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pragma once
#include <mutex>
#include <queue>
#include <shared_mutex>
namespace task_queue {
template <typename T>
class TaskQueue : protected std::queue<T> {
public:
using writelock = std::unique_lock<std::shared_mutex>;
using readlock = std::shared_lock<std::shared_mutex>;
TaskQueue() = default;
~TaskQueue() { clear(); }
TaskQueue(const TaskQueue&) = delete; // Delete copy constructor
TaskQueue(TaskQueue&&) = delete; // Delete move constructor
TaskQueue& operator=(const TaskQueue&) = delete; // Delete copy assign op
TaskQueue& operator=(TaskQueue&&) = delete; // Delete move assign op
bool empty() const {
readlock lock(mtx_);
return std::queue<T>::empty();
};
size_t size() const {
readlock lock(mtx_);
return std::queue<T>::size();
};
void clear() {
writelock lock(mtx_);
while (!std::queue<T>::empty())
std::queue<T>::pop();
}
void push(const T &obj) {
writelock lock(mtx_);
std::queue<T>::push(obj);
}
template <typename... Args>
void emplace(Args &&... args) {
writelock lock(mtx_);
std::queue<T>::emplace(std::forward<Args>(args)...); //perfect forwarding
}
// More like a try_pop
bool pop(T& holder) {
writelock lock(mtx_);
if (std::queue<T>::empty()) {
return false;
}
holder = std::move(std::queue<T>::front());
std::queue<T>::pop();
return true;
}
private:
mutable std::shared_mutex mtx_; // Mutable Can be changed in const function
};
} // namespace task_queue