-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadSafeQueue.hpp
More file actions
101 lines (86 loc) · 2.85 KB
/
Copy pathThreadSafeQueue.hpp
File metadata and controls
101 lines (86 loc) · 2.85 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <atomic>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <iostream>
template <typename T>
class ThreadSafeQueue {
struct Node {
std::shared_ptr<T> data;
std::unique_ptr<Node> next;
};
std::unique_ptr<Node> head;
Node* tail;
mutable std::mutex head_mutex;
mutable std::mutex tail_mutex;
mutable std::condition_variable data_cond;
Node * get_tail() {
std::lock_guard<std::mutex> l(tail_mutex);
return tail;
}
T pop_head() {
std::unique_ptr<Node> const old_head = std::move(head);
head=std::move(old_head->next);
return old_head;
}
std::unique_lock<std::mutex> wait_for_data() {
std::unique_lock<std::mutex> head_lock(head_mutex);
data_cond.wait(head_lock, [&] { return head.get() != get_tail(); });
return head_lock;
}
std::unique_ptr<Node> wait_pop_head(T& res) {
std::unique_lock<std::mutex> l(wait_for_data());
res = std::move(*head->data);
return pop_head();
}
std::unique_ptr<Node> wait_pop_head() {
std::unique_lock<std::mutex> l(wait_for_data());
return pop_head();
}
public:
ThreadSafeQueue(): head(new Node) {tail = head.get();}
ThreadSafeQueue(ThreadSafeQueue&&) = default;
ThreadSafeQueue& operator=(ThreadSafeQueue&&) = default;
ThreadSafeQueue(const ThreadSafeQueue&) = delete;
ThreadSafeQueue& operator=(const ThreadSafeQueue&) = delete;
bool try_pop(T & value) {
std::lock_guard<std::mutex> l(head_mutex);
if (head.get() == get_tail()) {
return false;
}
value = std::move(*head->data);
std::unique_ptr<Node> old_head = std::move(head);
head = std::move(old_head->next);
return true;
}
void push(T new_value) {
std::shared_ptr<T> new_data(
std::make_shared<T>(std::move(new_value)));
std::unique_ptr<Node> p(new Node);
{
std::lock_guard<std::mutex> tail_lock(tail_mutex);
tail->data=new_data;
Node* const new_tail=p.get();
tail->next=std::move(p);
tail=new_tail;
}
data_cond.notify_one();
}
void wait_and_pop(T& value) {
std::unique_ptr<Node> const old_head = wait_pop_head(value);
}
std::unique_ptr<T> wait_and_pop() {
std::unique_ptr<Node> const old_head = wait_pop_head();
return old_head->data;
}
void print() const {
std::lock_guard<std::mutex> lock_tail(tail_mutex); // prevent pushing new data
std::lock_guard<std::mutex> lock_head(head_mutex); // then prevent pop data
Node * node = head.get();
while (node != tail) {
std::cout << *node->data << " ";
node = node->next.get();
}
std::cout << std::endl;
}
};