-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadingTest.cpp
218 lines (184 loc) · 5.9 KB
/
ThreadingTest.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
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
#include <iostream>
#include <array>
#include <random>
#include <thread>
#include <shared_mutex>
#include <mutex>
#include <chrono>
#include <algorithm>
// A simple "database" with no locking that is dangerous to use across multiple threads
// The sleep_for statements simulate a more complex operation
class Database
{
public:
Database()
{
std::fill(data_.begin(), data_.end(), 0);
}
// Obtain a value
int readValue(size_t index) const
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
return data_.at(index);
}
// update a value
void updateValue(size_t index, int64_t value)
{
std::this_thread::sleep_for(std::chrono::milliseconds(5));
data_.at(index) += value;
}
// used for testing
bool isAllZero()
{
return std::all_of(data_.begin(), data_.end(), [](const int64_t &x) -> bool
{ return x == 0; });
}
static constexpr size_t DATABASE_SIZE = 10;
static constexpr auto DATABASE_TYPE = "non-threadsafe database";
friend std::ostream &operator<<(std::ostream &os, const Database &db);
protected:
std::array<int64_t, DATABASE_SIZE> data_;
};
// A database with simple locking using a mutex
// Threadsafe but slower
class SingleMutexDatabase : public Database
{
public:
SingleMutexDatabase() : Database(){};
int readValue(size_t index) const
{
std::lock_guard<std::mutex> lock{mutex_};
return Database::readValue(index);
}
void updateValue(size_t index, int64_t value)
{
std::lock_guard<std::mutex> lock{mutex_};
Database::updateValue(index, value);
}
static constexpr auto DATABASE_TYPE = "single mutex database";
private:
mutable std::mutex mutex_;
};
// A database using a shared_mutex allowing multiple reads at the same time
class SharedMutexDatabase : public Database
{
public:
SharedMutexDatabase() : Database(){};
int readValue(size_t index) const
{
// reads can oocurs simultaneously with a shared_lock
std::shared_lock lock{shared_mutex_};
return Database::readValue(index);
}
void updateValue(size_t index, int64_t value)
{
// writes require a unique_lock
std::unique_lock lock{shared_mutex_};
Database::updateValue(index, value);
}
static constexpr auto DATABASE_TYPE = "shared mutex database";
private:
mutable std::shared_mutex shared_mutex_;
};
// A database where access is controlled by multiple mutexes allowing
// some degree of shared access
class SplitMutexDatabase : public Database
{
public:
SplitMutexDatabase() : Database() {}
int readValue(size_t index) const
{
std::lock_guard<std::mutex> lock{
mutexes_[index % mutexes_.size()]};
return Database::readValue(index);
};
void updateValue(size_t index, int64_t value)
{
std::lock_guard<std::mutex> lock{
mutexes_[index % mutexes_.size()]};
Database::updateValue(index, value);
}
static constexpr auto DATABASE_TYPE = "split mutex database";
private:
mutable std::array<std::mutex, DATABASE_SIZE / 2> mutexes_;
};
std::ostream &
operator<<(std::ostream &os, const Database &db)
{
for (auto &i : db.data_)
{
os << i << " ";
}
return os;
}
std::array<size_t, Database::DATABASE_SIZE>
getIndicesInShuffledOrder(int seed)
{
std::array<size_t, Database::DATABASE_SIZE> indices;
std::iota(indices.begin(), indices.end(), 0);
std::mt19937 engine(seed);
std::shuffle(indices.begin(), indices.end(), engine);
return indices;
}
template <typename DBTYPE>
void readAllInRandomOrder(DBTYPE &database, int seed)
{
std::array<size_t, Database::DATABASE_SIZE> indices{getIndicesInShuffledOrder(seed)};
std::for_each(indices.begin(), indices.end(), [&](const size_t &i)
{ database.readValue(i);
std::this_thread::sleep_for(std::chrono::milliseconds(1)); });
}
template <typename DBTYPE>
void updateAllInRandomOrder(DBTYPE &database, int64_t value, int seed)
{
std::array<size_t, Database::DATABASE_SIZE> indices{getIndicesInShuffledOrder(seed)};
std::for_each(indices.begin(), indices.end(), [&](const size_t &i)
{ database.updateValue(i, value);
std::this_thread::sleep_for(std::chrono::milliseconds(10)); });
}
template <typename DBTYPE>
void testDatabase()
{
DBTYPE db;
std::vector<std::thread> threads;
auto startTime = std::chrono::high_resolution_clock::now();
for (size_t i = 0; i < 100; ++i)
{
threads.emplace_back([&]()
{
for (int repeat = 0; repeat < 1; ++repeat)
{
updateAllInRandomOrder(db, 25, threads.size());
updateAllInRandomOrder(db, -40, threads.size());
updateAllInRandomOrder(db, 15, threads.size());
} });
}
for (size_t i = 0; i < 1000; ++i)
{
threads.emplace_back([&]()
{
for (int repeat = 0; repeat < 1; ++repeat)
{
readAllInRandomOrder(db, threads.size());
} });
}
std::for_each(threads.begin(), threads.end(), [&](std::thread &t)
{
if (t.joinable())
{
t.join();
} });
auto endTime = std::chrono::high_resolution_clock::now();
std::cout << "Results for " << DBTYPE::DATABASE_TYPE << ":\n"
<< " Elapsed Time: " << std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count() << "ms\n"
<< " Database Contents: " << db << "\n"
<< " All Zero: " << (db.isAllZero() ? "Pass" : "FAILED!!!") << std::endl;
}
int main(int, char *[])
{
testDatabase<Database>();
testDatabase<SingleMutexDatabase>();
testDatabase<SharedMutexDatabase>();
testDatabase<SplitMutexDatabase>();
return 0;
}