-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeSnap-BlockingQueue.h.html
220 lines (209 loc) · 6.92 KB
/
CodeSnap-BlockingQueue.h.html
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!----------------------------------------------------------------------------
CodeSnap-BlockingQueue.htm
Published 19 Mar 2017
Jim Fawcett, CSE687 : Object Oriented Design, Summer 2017
Note:
- Markup characters in the text part, enclosed in <pre>...</pre> have to be
replaced with escape sequences, e.g., < becomes < and > becomes >
- Be careful that you don't replace genuine markup characters with escape
sequences, e.g., everything outside of the <pre>...</pre> section.
----------------------------------------------------------------------------->
<html>
<head>
<script src="js/ScriptsUtilities.js"></script>
<script src="js/ScriptsTemplate.js"></script>
<script src="js/ScriptsKeyboard.js"></script>
<script src="js/ScriptsMenuCpp.js"></script>
<link rel="stylesheet" href="css/StylesTemplate.css" />
<link rel="stylesheet" href="css/StylesMenu.css" />
<style>
h3 {
font-weight: normal;
}
</style>
</head>
<body id="github" onload="initializeMenu()">
<nav>
<div id="navbar"></div>
</nav>
<a id="Next" href="CodeSnap-BlockingQueue.cpp.html">N</a>
<a id="Prev" href="CodeSnap-BlockingQueue.txt.html">P</a>
<navKeys-Container>
<nav-Key id="sKey" onclick="toggleSwipeEvents()">S</nav-Key>
<nav-Key id="rKey" onclick="location.reload()">R</nav-Key>
<nav-Key id="tKey" onclick="scrollPageTop()">T</nav-Key>
<nav-Key id="bKey" onclick="scrollPageBottom()">B</nav-Key>
<nav-Key id="hKey" onclick="helpWin()">H</nav-Key>
<nav-Key id="pKey" onclick="loadPrev()">P</nav-Key>
<nav-Key id="nKey" onclick="loadNext()">N</nav-Key>
</navKeys-Container>
<h3>
<a href="CodeSnap-BlockingQueue.h.html">BlockingQueue.h</a>
<a href="CodeSnap-BlockingQueue.cpp.html">BlockingQueue.cpp</a>
<a href="CodeSnap-BlockingQueue.txt.html">BlockingQueue.txt</a>
<a href="https://github.com/JimFawcett/CppBlockingQueue">Code folder</a>
</h3>
<div class="indent">
This code illustrates how Packages should be structured<br />
and is also a good example of a template class.<br />
We will find BlockingQueue to be very useful when writing multi-threaded code.
</div>
<hr />
<pre class="codeSnap">
#ifndef CPP11_BLOCKINGQUEUE_H
#define CPP11_BLOCKINGQUEUE_H
///////////////////////////////////////////////////////////////
// Cpp11-BlockingQueue.h - Thread-safe Blocking Queue //
// ver 1.3 //
// Jim Fawcett, CSE687 - Object Oriented Design, Spring 2015 //
///////////////////////////////////////////////////////////////
/*
* Package Operations:
* -------------------
* This package contains one thread-safe class: BlockingQueue<T>.
* Its purpose is to support sending messages between threads.
* It is implemented using C++11 threading constructs including
* std::condition_variable and std::mutex. The underlying storage
* is provided by the non-thread-safe std::queue<T>.
*
* Required Files:
* ---------------
* Cpp11-BlockingQueue.h
*
* Build Process:
* --------------
* devenv Cpp11-BlockingQueue.sln /rebuild debug
*
* Maintenance History:
* --------------------
* ver 1.3 : 04 Mar 2016
* - changed behavior of front() to throw exception
* on empty queue.
* - added comment about std::unique_lock in deQ()
* ver 1.2 : 27 Feb 2016
* - added front();
* - added move ctor and move assignment
* - deleted copy ctor and copy assignment
* ver 1.1 : 26 Jan 2015
* - added copy constructor and assignment operator
* ver 1.0 : 03 Mar 2014
* - first release
*
*/
#include <condition_variable>
#include <mutex>
#include <thread>
#include <queue>
#include <string>
#include <iostream>
#include <sstream>
template <typename T>
class BlockingQueue {
public:
BlockingQueue() {}
BlockingQueue(BlockingQueue<T>&& bq);
BlockingQueue<T>& operator=(BlockingQueue<T>&& bq);
BlockingQueue(const BlockingQueue<T>&) = delete;
BlockingQueue<T>& operator=(const BlockingQueue<T>&) = delete;
T deQ();
void enQ(const T& t);
T& front();
void clear();
size_t size();
private:
std::queue<T> q_;
std::mutex mtx_;
std::condition_variable cv_;
};
//----< move constructor >---------------------------------------------
template<typename T>
BlockingQueue<T>::BlockingQueue(BlockingQueue<T>&& bq) // need to lock so can't initialize
{
std::lock_guard<std::mutex> l(mtx_);
q_ = bq.q_;
while (bq.q_.size() > 0) // clear bq
bq.q_.pop();
/* can't copy or move mutex or condition variable, so use default members */
}
//----< move assignment >----------------------------------------------
template<typename T>
BlockingQueue<T>& BlockingQueue<T>::operator=(BlockingQueue<T>&& bq)
{
if (this == &bq) return *this;
std::lock_guard<std::mutex> l(mtx_);
q_ = bq.q_;
while (bq.q_.size() > 0) // clear bq
bq.q_.pop();
/* can't move assign mutex or condition variable so use target's */
return *this;
}
//----< remove element from front of queue >---------------------------
template<typename T>
T BlockingQueue<T>::deQ()
{
std::unique_lock<std::mutex> l(mtx_);
/*
This lock type is required for use with condition variables.
The operating system needs to lock and unlock the mutex:
- when wait is called, below, the OS suspends waiting thread
and releases lock.
- when notify is called in enQ() the OS relocks the mutex,
resumes the waiting thread and sets the condition variable to
signaled state.
std::lock_quard does not have public lock and unlock functions.
*/
if(q_.size() > 0)
{
T temp = q_.front();
q_.pop();
return temp;
}
// may have spurious returns so loop on !condition
while (q_.size() == 0)
cv_.wait(l, [this] () { return q_.size() > 0; });
T temp = q_.front();
q_.pop();
return temp;
}
//----< push element onto back of queue >------------------------------
template<typename T>
void BlockingQueue<T>::enQ(const T& t)
{
{
std::unique_lock<std::mutex> l(mtx_);
q_.push(t);
}
cv_.notify_one();
}
//----< peek at next item to be popped >-------------------------------
template <typename T>
T& BlockingQueue<T>::front()
{
std::lock_guard<std::mutex> l(mtx_);
if(q_.size() > 0)
return q_.front();
throw std::exception("attempt to deQue empty queue");
}
//----< remove all elements from queue >-------------------------------
template <typename T>
void BlockingQueue<T>::clear()
{
std::lock_guard<std::mutex> l(mtx_);
while (q_.size() > 0)
q_.pop();
}
//----< return number of elements in queue >---------------------------
template<typename T>
size_t BlockingQueue<T>::size()
{
std::lock_guard<std::mutex> l(mtx_);
return q_.size();
}
#endif
</pre>
<!--<div class="photo">
<img src="pictures/CSTstrip.jpg" width="100%" />
</div>-->
<info-bar></info-bar>
</body>
</html>