-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLatches_02.cpp
More file actions
163 lines (119 loc) · 4.28 KB
/
Latches_02.cpp
File metadata and controls
163 lines (119 loc) · 4.28 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
// ===========================================================================
// Latches_02.cpp
// ===========================================================================
#include "../Logger/Logger.h"
#include <array>
#include <deque>
#include <future>
#include <iostream>
#include <latch>
#include <random>
#include <sstream>
#include <thread>
namespace Latches_03 {
constexpr size_t ThreadCount{ 4 };
constexpr size_t MaxDelay{ 5000 };
static int calcSumRange(int a, int b) {
int sum{};
for (int i{ a }; i != b; ++i) {
sum += i;
}
return sum;
}
static void example_latches_03()
{
std::latch done{ ThreadCount };
std::array<int, ThreadCount> results{};
std::vector<std::future<void>> tasks;
std::random_device device{};
auto worker = [&] (size_t index, size_t msecs, int first, int last) {
Logger::log(std::cout, "Calculating from ", first, " up to ", last, "...");
int result{ calcSumRange(first, last) };
results.at(index) = result;
// simulating still some calculation time ...
std::this_thread::sleep_for(std::chrono::milliseconds{ msecs });
Logger::log(std::cout, "Done");
done.count_down();
};
int begin{ 1 };
int increment{ 100 };
int end{ begin + increment };
for (size_t i{}; i != ThreadCount; ++i) {
size_t msecs{ static_cast<size_t>(device()) % MaxDelay };
std::future<void> future{
std::async(
worker,
i,
msecs,
begin,
end
)
};
tasks.push_back(std::move(future));
begin = end;
end += increment;
}
// block until work is done
done.wait();
Logger::log(std::cout, "All calculations done :)");
/*
* Kontrollfrage:
* Wie könnte man an dieser Stelle auf das Ende aller Worker-Threads warten
* ohne Verwendung eines std::latch-Objekts
* [tasks.clear();]
*/
// add partial results of worker threads
int total{};
for (size_t i{}; 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);
}
}
// =======================================================================
namespace Latches_04 {
constexpr size_t MaxDelay{ 3000 };
std::latch workDone{ 5 };
std::latch doExit{ 1 };
std::random_device device;
auto slave = [] (std::string name) {
Logger::log(std::cout, name, ": Started working.");
// simulating still some calculation time ...
size_t msecs{ 2000 + static_cast<size_t>(device()) % MaxDelay };
std::this_thread::sleep_for(std::chrono::milliseconds{ msecs });
// notify the master when work is done
Logger::log(std::cout, name, ": Work done!");
workDone.count_down();
// waiting before exiting ...
doExit.wait();
Logger::log(std::cout, name, ": Exit.");
};
static void example_latches_04()
{
std::deque<std::future<void>> tasks;
Logger::log(std::cout, "Working starts:");
tasks.push_back(std::async(std::launch::async, slave, "Worker (1)"));
tasks.push_back(std::async(std::launch::async, slave, "Worker (2)"));
tasks.push_back(std::async(std::launch::async, slave, "Worker (3)"));
tasks.push_back(std::async(std::launch::async, slave, "Worker (4)"));
tasks.push_back(std::async(std::launch::async, slave, "Worker (5)"));
workDone.wait();
Logger::log(std::cout, "Working done.");
doExit.count_down();
}
}
void test_latches_03()
{
using namespace Latches_03;
example_latches_03();
}
void test_latches_04()
{
using namespace Latches_04;
example_latches_04();
}
// ===========================================================================
// End-of-File
// ===========================================================================