-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBarriers_02.cpp
More file actions
201 lines (150 loc) · 7.29 KB
/
Barriers_02.cpp
File metadata and controls
201 lines (150 loc) · 7.29 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
// ===========================================================================
// Barriers_02.cpp
// ===========================================================================
#include "../Logger/Logger.h"
#include <array>
#include <barrier>
#include <deque>
#include <future>
#include <iostream>
#include <random>
#include <sstream>
#include <thread>
namespace Concurrency_Barriers_02 {
constexpr std::size_t ThreadCount{ 4 };
constexpr std::size_t MaxDelay{ 3000 };
static std::size_t calcSumRange(std::size_t a, std::size_t b) {
std::size_t sum{};
for (std::size_t i = a; i != b; ++i) {
sum += i;
}
return sum;
}
static void example_barriers_helper(std::array<std::size_t, ThreadCount>& results)
{
std::barrier sync{ ThreadCount };
std::vector<std::future<void>> tasks;
std::random_device device;
auto worker = [&](std::size_t i, std::size_t msecs, std::size_t first, std::size_t last) {
Logger::log(std::cout, "Calculating from ", first, " up to ", last, "...");
std::size_t result = calcSumRange(first, last);
results.at(i) = result;
// simulating still some calculation time ...
std::this_thread::sleep_for(std::chrono::milliseconds{ msecs });
sync.arrive_and_wait();
Logger::log(std::cout, "All calculations done :)");
};
std::size_t begin{ 1 };
std::size_t increment{ 100 };
std::size_t end = begin + increment;
for (std::size_t i = 0; i != ThreadCount; ++i) {
std::size_t msecs = static_cast<std::size_t>(device()) % MaxDelay;
std::future<void> future = std::async(
std::launch::async,
worker,
i,
msecs,
begin,
end
);
tasks.push_back(std::move(future));
begin = end;
end += increment;
}
// Note:
// the destructors for the std::async objects will wait
// for them all to finish at the end of this function !
}
static void example_barriers_01()
{
std::array<std::size_t, ThreadCount> results{ 0 };
example_barriers_helper(results);
// add partial results of worker threads
std::size_t total = 0;
for (std::size_t i = 0; i != ThreadCount; ++i) {
total += results.at(i);
Logger::log(std::cout, "Partial result: ", results.at(i));
}
// use gauss to verify : n * (n + 1) / 2 ==> 80200, if n == 4
Logger::log(std::cout, "Total: ", total);
}
// =======================================================================
constexpr std::size_t WorkersCount{ 6 };
std::barrier workDoneBarrier(WorkersCount);
std::random_device device;
auto fulltimeWorker = [](std::string name, std::size_t delay) {
// simulating still some calculation time ...
Logger::log(std::cout, name, ": Forenoon work starting!");
std::this_thread::sleep_for(std::chrono::milliseconds{ delay });
Logger::log(std::cout, name, ": Forenoon work done!");
workDoneBarrier.arrive_and_wait(); // wait until morning work has completed of all workers
Logger::log(std::cout, name, ": Afternoon work starting!");
std::this_thread::sleep_for(std::chrono::milliseconds{ delay });
Logger::log(std::cout, name, ": Afternoon work done!");
};
enum class PartTime { Forenoon, Afternoon };
auto parttimeWorker = [](PartTime parttime, std::string name, std::size_t delay) {
if (parttime == PartTime::Forenoon) {
Logger::log(std::cout, name, ": Forenoon work starting!");
std::this_thread::sleep_for(std::chrono::milliseconds{ delay });
Logger::log(std::cout, name, ": Forenoon work done!");
workDoneBarrier.arrive_and_drop(); // don't participate in afternoon working
}
else if (parttime == PartTime::Afternoon) {
workDoneBarrier.arrive_and_wait(); // wait until morning work has completed of all workers
Logger::log(std::cout, name, ": Afternoon work starting!");
std::this_thread::sleep_for(std::chrono::milliseconds{ delay });
Logger::log(std::cout, name, ": Afternoon work done!");
}
};
// fulltime and forenoon workers
static void example_barriers_02_a() {
std::deque<std::future<void>> tasks;
std::size_t msecs;
msecs = static_cast<std::size_t>(device()) % MaxDelay;
tasks.push_back(std::async(std::launch::async, parttimeWorker, PartTime::Forenoon, "forenoonWorker (a)", msecs));
msecs = static_cast<std::size_t>(device()) % MaxDelay;
tasks.push_back(std::async(std::launch::async, parttimeWorker, PartTime::Forenoon, "forenoonWorker (b)", msecs));
msecs = static_cast<std::size_t>(device()) % MaxDelay;
tasks.push_back(std::async(std::launch::async, parttimeWorker, PartTime::Forenoon, "forenoonWorker (c)", msecs));
msecs = static_cast<std::size_t>(device()) % MaxDelay;
tasks.push_back(std::async(std::launch::async, fulltimeWorker, "fulltimeWorker (1)", msecs));
msecs = static_cast<std::size_t>(device()) % MaxDelay;
tasks.push_back(std::async(std::launch::async, fulltimeWorker, "fulltimeWorker (2)", msecs));
msecs = static_cast<std::size_t>(device()) % MaxDelay;
tasks.push_back(std::async(std::launch::async, fulltimeWorker, "fulltimeWorker (3)", msecs));
}
// fulltime and afternoon workers
static void example_barriers_02_b() {
std::deque<std::future<void>> tasks;
std::size_t msecs;
msecs = static_cast<std::size_t>(device()) % MaxDelay;
tasks.push_back(std::async(std::launch::async, fulltimeWorker, "fulltimeWorker (1)", msecs));
msecs = static_cast<std::size_t>(device()) % MaxDelay;
tasks.push_back(std::async(std::launch::async, fulltimeWorker, "fulltimeWorker (2)", msecs));
msecs = static_cast<std::size_t>(device()) % MaxDelay;
tasks.push_back(std::async(std::launch::async, fulltimeWorker, "fulltimeWorker (3)", msecs));
msecs = static_cast<std::size_t>(device()) % MaxDelay;
tasks.push_back(std::async(std::launch::async, parttimeWorker, PartTime::Afternoon, "afternoonWorker (A)", msecs));
msecs = static_cast<std::size_t>(device()) % MaxDelay;
tasks.push_back(std::async(std::launch::async, parttimeWorker, PartTime::Afternoon, "afternoonWorker (B)", msecs));
msecs = static_cast<std::size_t>(device()) % MaxDelay;
tasks.push_back(std::async(std::launch::async, parttimeWorker, PartTime::Afternoon, "afternoonWorker (C)", msecs));
}
static void example_barriers_02()
{
Logger::log(std::cout, "Working starts [PartimeWorker & FulltimeWorker]:");
example_barriers_02_a();
example_barriers_02_b();
Logger::log(std::cout, "Working ends starts [PartimeWorker & FulltimeWorker].");
}
}
void test_barriers_02()
{
using namespace Concurrency_Barriers_02;
example_barriers_01();
example_barriers_02();
}
// ===========================================================================
// End-of-File
// ===========================================================================