-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path01_RequestStop.cpp
More file actions
197 lines (154 loc) · 5.1 KB
/
01_RequestStop.cpp
File metadata and controls
197 lines (154 loc) · 5.1 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
// ===========================================================================
// RequestStop.cpp - JThread is cooperatively interruptible
// ===========================================================================
#include "../Logger/Logger.h"
#include <atomic>
#include <chrono>
#include <iostream>
#include <stop_token>
#include <thread>
namespace JoinableThreadCooperativeInterruptibility {
static void sleep_for_seconds(int seconds)
{
std::this_thread::sleep_for(std::chrono::seconds{ seconds });
}
// =============================================
// Szenario 1:
// class std::thread -- endless loop
static void jthread_01()
{
Logger::log(std::cout, "Starting main thread ...");
std::thread t{
[]() {
while (true) {
Logger::log(std::cout, "Working ...");
sleep_for_seconds(1);
}
}
};
sleep_for_seconds(5);
Logger::log(std::cout, "Leaving main thread ...");
t.join();
}
// =============================================
// Szenario 2:
// class std::jthread -- endless loop
static void jthread_02()
{
Logger::log(std::cout, "Starting main thread ...");
std::jthread jt{
[]() {
while (true) {
Logger::log(std::cout, "Working ...");
sleep_for_seconds(1);
}
}
};
sleep_for_seconds(5);
Logger::log(std::cout, "Leaving main thread ...");
}
// =============================================
// Szenario 3:
// class std::jthread -- using request_stop -- still endless loop
static void jthread_03()
{
Logger::log(std::cout, "Starting main thread ...");
std::jthread jt{
[]() {
while (true) {
Logger::log(std::cout, "Working ...");
sleep_for_seconds(1);
}
}
};
sleep_for_seconds(5);
jt.request_stop();
Logger::log(std::cout, "Leaving main thread ...");
}
// =============================================
// Szenario 4:
// class std::jthread -- using request_stop -- no more endless loop
static void jthread_04()
{
Logger::log(std::cout, "Starting main thread ...");
std::jthread jt{
[](std::stop_token token) {
while (!token.stop_requested()) {
Logger::log(std::cout, "Working ...");
sleep_for_seconds(1);
}
}
};
sleep_for_seconds(5);
jt.request_stop();
Logger::log(std::cout, "Leaving main thread ...");
}
// =============================================
// Szenario 5:
// class std::jthread -- using request_stop
static void jthread_05()
{
Logger::log(std::cout, "Starting main thread ...");
std::jthread jt{
[](std::stop_token token) {
std::atomic<bool> running { true };
// called on a stop request
std::stop_callback callback {
token,
[&]() {
Logger::log(std::cout, "Stop requested");
running = false;
}
};
while (running) {
Logger::log(std::cout, "Working ...");
sleep_for_seconds(1);
}
}
};
sleep_for_seconds(5);
jt.request_stop();
Logger::log(std::cout, "Leaving main thread ...");
}
// =============================================
// Szenario 6:
// class std::jthread -- using request_stop -- introducing std::stop_source
static void jthread_06()
{
Logger::log(std::cout, "Starting main thread ...");
std::jthread jt{
[](std::stop_token token) {
std::atomic<bool> running { true };
// called on a stop request
std::stop_callback callback {
token,
[&]() {
Logger::log(std::cout, "Stop requested");
running = false;
}
};
while (running) {
Logger::log(std::cout, "Working ...");
sleep_for_seconds(1);
}
}
};
sleep_for_seconds(5);
std::stop_source source{ jt.get_stop_source() };
source.request_stop(); // request stop on stop token of thread 'jt'
Logger::log(std::cout, "Leaving main thread ...");
}
}
void test_joinable_thread_cooperative_interruptibility()
{
using namespace JoinableThreadCooperativeInterruptibility;
jthread_01();
jthread_02();
jthread_03();
jthread_04();
jthread_05();
jthread_06();
}
// ===========================================================================
// End-of-File
// ===========================================================================