forked from apolukhin/course-nimble_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mt_3.hpp
151 lines (115 loc) · 3.63 KB
/
mt_3.hpp
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
#include "util.hpp"
#include <deque>
//////////////////////////// TASK 3 ////////////////////////////
template <class T>
class naive_queue_t {
std::deque<T> data_;
std::mutex mutex_;
std::condition_variable cond_;
public:
using value_type = T;
void push(const T& val) {
std::lock_guard<std::mutex> l{mutex_};
data_.push_back(val);
cond_.notify_one();
}
T pop() {
std::unique_lock<std::mutex> lock{mutex_};
while (data_.empty()) {
cond_.wait(lock);
}
T res = data_.front();
data_.pop_front();
return res;
}
};
template <class T>
class optim_queue_t {
std::deque<T> data_;
std::mutex mutex_;
std::condition_variable cond_;
public:
using value_type = T;
void push(const T& val) {
std::lock_guard<std::mutex> l{mutex_};
data_.push_back(val);
cond_.notify_one();
}
T pop() {
std::unique_lock<std::mutex> lock{mutex_};
while (data_.empty()) {
cond_.wait(lock);
}
T res = data_.front();
data_.pop_front();
return res;
}
};
template <class T>
class ultim_queue_t {
std::deque<T> data_;
std::mutex mutex_;
std::condition_variable cond_;
public:
using value_type = T;
void push(const T& val) {
std::lock_guard<std::mutex> l{mutex_};
data_.push_back(val);
cond_.notify_one();
}
T pop() {
std::unique_lock<std::mutex> lock{mutex_};
while (data_.empty()) {
cond_.wait(lock);
}
T res = data_.front();
data_.pop_front();
return res;
}
};
//////////////////////////// DETAIL ////////////////////////////
naive_queue_t<std::vector<int>> naive_queue;
optim_queue_t<std::vector<int>> optim_queue;
ultim_queue_t<std::vector<int>> ultim_queue;
template <class Queue, class T>
static void mt_queue(benchmark::State& state, Queue& q, const T& init_val) {
const std::size_t iterations_count = 8 << 10;
const std::size_t readers_count = state.range(0);
const auto pop_in_loop = [&q, readers_count, iterations_count](){
for (unsigned i = 0; i < iterations_count / readers_count; ++i) {
const auto val = q.pop();
benchmark::DoNotOptimize(val);
}
};
const std::size_t writers_count = state.range(1);
const auto push_in_loop = [&q, writers_count, &init_val, iterations_count](){
for (unsigned i = 0; i < iterations_count / writers_count; ++i) {
q.push(init_val);
}
};
std::vector<std::thread> readers;
readers.resize(readers_count);
std::vector<std::thread> writers;
writers.resize(writers_count - 1);
for (auto _ : state) {
state.PauseTiming();
for (unsigned i = 0; i < readers_count; ++i){
readers[i] = std::thread{pop_in_loop};
}
state.ResumeTiming();
for (unsigned i = 0; i < writers_count - 1; ++i){
writers[i] = std::thread{push_in_loop};
}
push_in_loop();
for (auto& t : readers){
t.join();
}
for (auto& t : writers){
t.join();
}
}
}
const std::vector<int> init_value{1, 2, 42, 314};
BENCH(mt_queue, naive/rw, naive_queue, init_value)->RangeMultiplier(2)->RangePair(1, 4, 1, 4); //->UseRealTime();
BENCH(mt_queue, optim/rw, optim_queue, init_value)->RangeMultiplier(2)->RangePair(1, 4, 1, 4); //->UseRealTime();
BENCH(mt_queue, ultim/rw, ultim_queue, init_value)->RangeMultiplier(2)->RangePair(1, 4, 1, 4); //->UseRealTime();