-
Notifications
You must be signed in to change notification settings - Fork 34
/
threadsafeq.cpp
163 lines (139 loc) · 4.15 KB
/
threadsafeq.cpp
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// derived from http://www.slideshare.net/AntoineSavine/multithreading-c
#include <queue>
#include <mutex>
#include <condition_variable> // GB - needed for tsqueue Pop that waits if q empty
// What is the difference between lock_guard and unique_lock?
// The difference is that you can lock and unlock a std::unique_lock.
// Must use a std::unique_lock for condition variables.
// std::lock_guard will be locked only once on construction and unlocked on destruction.
// Always use lock_guard, unless you need the capabilities of unique_lock.
// A condition_variable needs a unique_lock.
template <typename T>
class tsQueue { // thread safe queue
protected:
std::queue<T> q;
mutable std::mutex m;
public:
virtual ~tsQueue() {
std::lock_guard<std::mutex> smartLock(m);
while( not q.empty() ) {
q.pop();
}
}
bool empty() const {
std::lock_guard<std::mutex> smartLock(m);
return q.empty();
}
void push( T& t ) {
std::lock_guard<std::mutex> smartLock(m);
q.push( std::move( t ) );
}
bool tryPop( T& t ) {
std::lock_guard<std::mutex> smartLock(m);
if(q.empty()) return false;
t = std::move( q.front() );
q.pop();
return true;
}
};
template <typename T> // GB
class tsQueueWait : public tsQueue<T> { // tsqueue Pop that waits for Push if q empty
std::condition_variable cv;
bool shutdown;
public:
tsQueueWait() : shutdown(false) {}
virtual ~tsQueueWait() {
shutdown = true;
cv.notify_all();
}
void Push( T& t ) {
std::lock_guard<std::mutex> smartLock(tsQueue<T>::m);
tsQueue<T>::q.push( std::move( t ) );
cv.notify_one();
}
bool Pop( T& t ) {
std::unique_lock<std::mutex> smartLock(tsQueue<T>::m);
while(tsQueue<T>::q.empty() && not shutdown)
cv.wait(smartLock);
if(shutdown) return false;
t = std::move( tsQueue<T>::q.front() );
tsQueue<T>::q.pop();
return true;
}
};
// includes for test program
#include <string>
#include <vector>
#include <thread>
#include <iostream>
#include <sstream>
int main()
{
// runtime error:
// terminate called without an active exception
// Declaring 'std::vector<std::thread> tidVector(N);' was the problem.
// Works fine declared as 'std::vector<std::thread> tidVector;'
// Reason: queues N thread reads and N thread writes
// vector<thread> tidVector(N) should be 2*N, not N
const int N = 1000;
try {
// tsQueue<int> q;
tsQueueWait<int> q; // default code works with tsQueueWait
std::vector<std::thread> tidVector;
auto pushQ= [&q] (int i) {q.push(i);};
auto popQ = [&q] (int i)
{
int tries = 1;
int ii;
while(q.tryPop(ii) == false)
tries++;
std::string s = "thread " + std::to_string(i) + " popped " + std::to_string(ii);
if(tries != 1) {
s += " after " + std::to_string(tries) + " tries";
}
s += "\n";
std::cout << s;
};
// test out the tryPop mechanism.
// queue the pops before the pushes.
// Q N reads
for(int i = 0; i < N ; i++) {
tidVector.push_back( std::thread( popQ, i ) );
}
// issue N writes
for(int i = 0; i < N ; i++) {
tidVector.push_back( std::thread( pushQ, i ) );
}
for(auto& e : tidVector) e.join();
std::cout << "\n";
} catch(std::exception& e) {
std::cerr << "program threw: " << e.what() << "\n";
} catch(...) {
std::cerr << "program threw unspecified error" << "\n";
}
try {
tsQueueWait<int> q;
std::vector<std::thread> tidVector;
auto PushQ= [&q] (int t) {q.Push(t);};
auto PopQ = [&q] (int t)
{
int i;
q.Pop(i);
std::cout << "thread " + std::to_string(t) + " popped " + std::to_string(i) + " (after possible wait)\n";
};
// Q N reads
for(int i = 0; i < N ; i++) {
tidVector.push_back( std::thread( PopQ, i ) );
}
// issue N writes
for(int i = 0; i < N ; i++) {
tidVector.push_back( std::thread( PushQ, i ) );
}
for(auto& e : tidVector) e.join();
} catch(std::exception& e) {
std::cerr << "program threw: " << e.what() << "\n";
} catch(...) {
std::cerr << "program threw unspecified error" << "\n";
}
return 0;
}