-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathTestLockingPolicy.cpp
More file actions
218 lines (174 loc) · 4.84 KB
/
TestLockingPolicy.cpp
File metadata and controls
218 lines (174 loc) · 4.84 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
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
/*
* TestLockingPolicy.cpp
*
* Created on: Nov 13, 2021
* Author: <a href="mailto:damirlj@yahoo.com">Damir Ljubic</a>
*/
#include <string>
#include <vector>
#include <thread>
#include <iostream>
#include <algorithm>
#include <chrono>
#include <atomic>
// Different locking policies
#include "NonLock.h"
#include "ObjecLevelLock.h"
#include "ClassLevelLock.h"
#include "TestLockingPolicy.h"
/**
* A basic example of using
* single policy which is plugged-in into the host class
*
* @tparam LockingPolicy
*/
template <class LockingPolicy>
class Message : private LockingPolicy
{
public:
Message() = default;
explicit Message(std::string msg) noexcept : m_msg(std::move(msg))
{}
friend std::ostream& operator << (std::ostream& out, const Message& msg)
{
{
typename LockingPolicy::Lock lock {msg};
out << msg.get() << '\n';
}
return out;
}
void set(const std::string& msg)
{
typename LockingPolicy::Lock lock {*this};
m_msg = msg;
}
std::string get() const { return m_msg;}
private:
std::string m_msg;
};
namespace
{
/**
* Class-level locking.
*
* The all instances of the class shared the same
* locking mechanism (static mutex)
*
*/
[[maybe_unused]]
void testClassLevelLock()
{
using namespace std;
using namespace utils::locking;
using namespace std::chrono_literals;
using message_t = Message<ClassLevelLock>;
//using message_t = Message<NonLock>; // compare when locking is disabled (single-thread environment)
vector<thread> threads;
constexpr size_t numOfThreads = 10;
std::atomic<bool> done = false;
// Launch background threads
for (size_t i = 0; i < numOfThreads; ++i)
{
threads.emplace_back(
[&done] // thread function
{
for (;;)
{
if (done) break;
for (auto j = 0; j < 5; ++j )
{
message_t msg {std::string(100, '=')};
std::cout << msg;
}
std::this_thread::sleep_for(100ms);
}
}
);
}
// main thread
for (auto j = 0; j < 10; ++j)
{
message_t msg {std::string(100, '8')};
std::cout << msg;
}
std::this_thread::sleep_for(1s);
done = true;
std::for_each(threads.begin(), threads.end(), [](auto& thread){
thread.join();
});
std::cout << "\n\nAll threads joined...\n";
}
}
namespace
{
/**
* Object-level locking.
*
* Every instance of the class has its own locking mechanism
* (per-object mutex)
*/
[[maybe_unused]]
void testObjectLevelLock()
{
using namespace std;
using namespace utils::locking;
using namespace std::chrono_literals;
/*
* Share the object between threads
*/
using message_t = Message<ObjectLevelLock>;
//using message_t = Message<NonLock>; // compare when locking is disabled (single-thread environment)
auto msg = std::make_shared<message_t>();
vector<thread> threads;
constexpr size_t numOfThreads = 10;
std::atomic<bool> done = false;
// Launch background threads
for (size_t i = 0; i < numOfThreads; ++i)
{
threads.emplace_back(
[&done, &msg]() mutable // thread function
{
for (;;)
{
if (done) break;
for (auto j = 0; j < 5; ++j )
{
msg->set(std::string(100, '='));
std::cout << *msg;
}
std::this_thread::sleep_for(100ms);
}
}
);
}
// main thread
for (auto j = 0; j < 10; ++j)
{
msg->set(std::string(100, '8'));
std::cout << *msg;
}
std::this_thread::sleep_for(1s);
done = true;
std::for_each(threads.begin(), threads.end(), [](auto& thread){
thread.join();
});
std::cout << "\n\nAll threads joined...\n";
}
}
namespace utils::locking
{
/*
* test in your main.cpp
*
* int main()
* {
* return utils::locking::testLockingPolicy();
* }
*/
int testLockingPolicy()
{
testClassLevelLock();
//testObjectLevelLock();
return 0;
}
}