-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLatches_01.cpp
More file actions
118 lines (87 loc) · 3.3 KB
/
Latches_01.cpp
File metadata and controls
118 lines (87 loc) · 3.3 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
// ===========================================================================
// Latches_01.cpp
// ===========================================================================
#include "../Logger/Logger.h"
#include <chrono> // std::chrono::milliseconds
#include <cstddef> // std::size_t
#include <iostream> // std::cout
#include <latch> // std::latch
#include <thread> // std::jthread, std::this_thread
#include <vector> // std::vector
namespace Latches_01 {
static void loop(char ch, std::size_t count) {
for (std::size_t j{}; j != count; ++j)
{
// loop printing the char ch
std::cout.put(ch);
std::cout.flush();
std::this_thread::sleep_for(std::chrono::milliseconds{ 100 });
}
}
static void example_latches_01()
{
std::latch allDone{ 2 };
std::cout << "Waiting until all tasks are done:" << std::endl;
// start two threads dealing with printing chars
std::jthread t1 {
[&] () {
loop('!', 30);
// signal that this task is done
allDone.count_down(); // atomically decrement counter of latch
}
};
std::jthread t2 {
[&] () {
loop('?', 30);
// signal that this task is done
allDone.count_down(); // atomically decrement counter of latch
}
};
// wait until all tasks have finished
allDone.wait();
std::cout << std::endl << "All tasks done."<< std::endl;
}
static void example_latches_02()
{
Logger::log(std::cout, "Start:");
std::size_t numThreads{ 10 };
// initialize latch to start the threads
// when all of them have been initialized
std::latch allReady{ static_cast<ptrdiff_t>(numThreads) };
// start numThreads threads:
std::vector<std::jthread> threads;
for (std::size_t i{}; i != numThreads; ++i) {
std::jthread t{
[i, &allReady]() {
Logger::log(std::cout, "JThread ", i, " started (", std::this_thread::get_id(), ")");
// initialize each thread (simulate to take some time):
std::this_thread::sleep_for(std::chrono::milliseconds{ 700 * i });
Logger::log(std::cout, "JThread ", i, " waiting for execution");
// synchronize threads so that all start together here
allReady.count_down();
allReady.wait();
// or
// allReady.arrive_and_wait();
// perform whatever the thread does (loop printing its index)
char ch{ static_cast<char>('0' + i) };
loop(ch, 10);
}
};
threads.push_back(std::move(t));
}
Logger::log(std::cout, "Done.");
}
}
void test_latches_01()
{
using namespace Latches_01;
example_latches_01();
}
void test_latches_02()
{
using namespace Latches_01;
example_latches_02();
}
// ===========================================================================
// End-of-File
// ===========================================================================