-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeSnap-BlockingQueue.cpp.html
More file actions
148 lines (135 loc) · 5.05 KB
/
CodeSnap-BlockingQueue.cpp.html
File metadata and controls
148 lines (135 loc) · 5.05 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
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
<!----------------------------------------------------------------------------
CodeSnap-BlockingQueue.cpp.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.txt.html">N</a>
<a id="Prev" href="CodeSnap-BlockingQueue.h.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">
///////////////////////////////////////////////////////////////
// Cpp11-BlockingQueue.cpp - Thread-safe Blocking Queue //
// ver 1.3 //
// Jim Fawcett, CSE687 - Object Oriented Design, Spring 2013 //
///////////////////////////////////////////////////////////////
#include <condition_variable>
#include <mutex>
#include <thread>
#include <queue>
#include <string>
#include <iostream>
#include <sstream>
#include "Cpp11-BlockingQueue.h"
#ifdef TEST_BLOCKINGQUEUE
std::mutex ioLock;
void test(BlockingQueue<std::string>* pQ)
{
std::string msg;
do
{
msg = pQ->deQ();
{
std::lock_guard<std::mutex> l(ioLock);
std::cout << "\n thread deQed " << msg.c_str();
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
} while(msg != "quit");
}
int main()
{
std::cout << "\n Demonstrating C++11 Blocking Queue";
std::cout << "\n ====================================";
BlockingQueue<std::string> q;
std::thread t(test, &q);
for(int i=0; i<15; ++i)
{
std::ostringstream temp;
temp << i;
std::string msg = std::string("msg#") + temp.str();
{
std::lock_guard<std::mutex> l(ioLock);
std::cout << "\n main enQing " << msg.c_str();
}
q.enQ(msg);
std::this_thread::sleep_for(std::chrono::milliseconds(3));
}
q.enQ("quit");
t.join();
std::cout << "\n";
std::cout << "\n Making move copy of BlockingQueue";
std::cout << "\n -----------------------------------";
std::string msg = "test";
q.enQ(msg);
std::cout << "\n before move:";
std::cout << "\n q.size() = " << q.size();
std::cout << "\n q.front() = " << q.front();
BlockingQueue<std::string> q2 = std::move(q); // move assignment
std::cout << "\n after move:";
std::cout << "\n q2.size() = " << q2.size();
std::cout << "\n q.size() = " << q.size();
std::cout << "\n q2 element = " << q2.deQ() << "\n";
std::cout << "\n Move assigning state of BlockingQueue";
std::cout << "\n ---------------------------------------";
BlockingQueue<std::string> q3;
q.enQ("test");
std::cout << "\n before move:";
std::cout << "\n q.size() = " << q.size();
std::cout << "\n q.front() = " << q.front();
q3 = std::move(q);
std::cout << "\n after move:";
std::cout << "\n q.size() = " << q.size();
std::cout << "\n q3.size() = " << q3.size();
std::cout << "\n q3 element = " << q3.deQ() << "\n";
std::cout << "\n\n";
}
#endif
</pre>
<!--<div class="photo">
<img src="pictures/CSTstrip.jpg" width="100%" />
</div>-->
<info-bar></info-bar>
</body>
</html>