FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
jthread/source/test_jthread1.cpp at master · dmitriano/jthread · GitHub
dmitriano
/
jthread
Public
forked from
josuttis/jthread
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
jthread
/
source
/
test_jthread1.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
477 lines (443 loc) · 17.5 KB
Breadcrumbs
jthread
/
source
/
test_jthread1.cpp
Copy path
File metadata and controls
477 lines (443 loc) · 17.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#
include
"
jthread.hpp
"
#
include
<
iostream
>
#
include
<
string
>
#
include
<
chrono
>
#
include
<
cassert
>
#
include
<
atomic
>
using
namespace
::std::literals
;
//
------------------------------------------------------
void
testJThreadWithout
()
{
//
test the basic jthread API (not taking stop_token arg)
std::cout <<
"
*** start testJThreadWithout()
"
<< std::endl;
assert
(
std::jthread::hardware_concurrency
() ==
std::thread::hardware_concurrency
());
std::stop_token stoken;
assert
(!stoken.
stop_possible
());
{
std::jthread::id t1ID{
std::this_thread::get_id
()};
std::atomic<
bool
> t1AllSet{
false
};
std::jthread
t1
([&t1ID, &t1AllSet] {
//
NOTE: no stop_token passed
//
check some values of the started thread:
t1ID =
std::this_thread::get_id
();
t1AllSet.
store
(
true
);
//
wait until loop is done (no interrupt checked)
for
(
int
c=
'
9
'
; c>=
'
0
'
; --c) {
std::this_thread::sleep_for
(222ms);
std::cout.
put
(
static_cast
<
char
>(c)).
flush
();
}
std::cout <<
"
END t1
"
<< std::endl;
});
//
wait until t1 has set all initial values:
for
(
int
i=
0
; !t1AllSet.
load
(); ++i) {
std::this_thread::sleep_for
(10ms);
std::cout.
put
(
'
o
'
).
flush
();
}
//
and check all values:
assert
(t1.
joinable
());
assert
(t1ID == t1.
get_id
());
stoken = t1.
get_stop_token
();
assert
(!stoken.
stop_requested
());
}
//
leave scope of t1 without join() or detach() (signals cancellation)
assert
(stoken.
stop_requested
());
std::cout <<
"
\n
*** OK
"
<< std::endl;
}
//
------------------------------------------------------
void
testThreadWithToken
()
{
//
test the basic thread API (taking stop_token arg)
std::cout <<
"
*** start testThreadWithToken()
"
<< std::endl;
std::stop_source ssource;
std::stop_source origsource;
assert
(ssource.
stop_possible
());
assert
(!ssource.
stop_requested
());
{
std::jthread::id t1ID{
std::this_thread::get_id
()};
std::atomic<
bool
> t1AllSet{
false
};
std::atomic<
bool
> t1done{
false
};
std::jthread
t1
([&t1ID, &t1AllSet, &t1done] (std::stop_token st) {
//
check some values of the started thread:
t1ID =
std::this_thread::get_id
();
t1AllSet.
store
(
true
);
//
wait until interrupt is signaled (due to destructor of t1):
for
(
int
i=
0
; !st.
stop_requested
(); ++i) {
std::this_thread::sleep_for
(100ms);
std::cout.
put
(
'
.
'
).
flush
();
}
t1done.
store
(
true
);
std::cout <<
"
END t1
"
<< std::endl;
},
ssource.
get_token
());
//
wait until t1 has set all initial values:
for
(
int
i=
0
; !t1AllSet.
load
(); ++i) {
std::this_thread::sleep_for
(10ms);
std::cout.
put
(
'
o
'
).
flush
();
}
//
and check all values:
assert
(t1.
joinable
());
assert
(t1ID == t1.
get_id
());
std::this_thread::sleep_for
(470ms);
origsource =
std::move
(ssource);
ssource = t1.
get_stop_source
();
assert
(!ssource.
stop_requested
());
auto
ret = ssource.
request_stop
();
assert
(ret);
ret = ssource.
request_stop
();
assert
(!ret);
assert
(ssource.
stop_requested
());
assert
(!t1done.
load
());
assert
(!origsource.
stop_requested
());
std::this_thread::sleep_for
(470ms);
origsource.
request_stop
();
}
//
leave scope of t1 without join() or detach() (signals cancellation)
assert
(origsource.
stop_requested
());
assert
(ssource.
stop_requested
());
std::cout <<
"
\n
*** OK
"
<< std::endl;
}
//
------------------------------------------------------
void
testJoin
()
{
//
test jthread join()
std::cout <<
"
\n
*** start testJoin()
"
<< std::endl;
std::stop_source ssource;
assert
(ssource.
stop_possible
());
{
std::jthread
t1
([](std::stop_token stoken) {
//
wait until interrupt is signaled (due to calling request_stop() for the token):
for
(
int
i=
0
; !stoken.
stop_requested
(); ++i) {
std::this_thread::sleep_for
(100ms);
std::cout.
put
(
'
.
'
).
flush
();
}
std::cout <<
"
END t1
"
<< std::endl;
});
ssource = t1.
get_stop_source
();
//
let nother thread signal cancellation after some time:
std::jthread
t2
([ssource] ()
mutable
{
for
(
int
i=
0
; i <
10
; ++i) {
std::this_thread::sleep_for
(70ms);
std::cout.
put
(
'
x
'
).
flush
();
}
//
signal interrupt to other thread:
ssource.
request_stop
();
std::cout <<
"
END t2
"
<< std::endl;
});
//
wait for all thread to finish:
t2.
join
();
assert
(!t2.
joinable
());
assert
(t1.
joinable
());
t1.
join
();
assert
(!t1.
joinable
());
}
//
leave scope of t1 without join() or detach() (signals cancellation)
std::cout <<
"
\n
*** OK
"
<< std::endl;
}
//
------------------------------------------------------
void
testDetach
()
{
//
test jthread detach()
std::cout <<
"
\n
*** start testDetach()
"
<< std::endl;
std::stop_source ssource;
assert
(ssource.
stop_possible
());
std::atomic<
bool
> t1FinallyInterrupted{
false
};
{
std::jthread t0;
std::jthread::id t1ID{
std::this_thread::get_id
()};
bool
t1IsInterrupted;
std::stop_token t1InterruptToken;
std::atomic<
bool
> t1AllSet{
false
};
std::jthread
t1
([&t1ID, &t1IsInterrupted, &t1InterruptToken, &t1AllSet, &t1FinallyInterrupted]
(std::stop_token stoken) {
//
check some values of the started thread:
t1ID =
std::this_thread::get_id
();
t1InterruptToken = stoken;
t1IsInterrupted = stoken.
stop_requested
();
assert
(stoken.
stop_possible
());
assert
(!stoken.
stop_requested
());
t1AllSet.
store
(
true
);
//
wait until interrupt is signaled (due to calling request_stop() for the token):
for
(
int
i=
0
; !stoken.
stop_requested
(); ++i) {
std::this_thread::sleep_for
(100ms);
std::cout.
put
(
'
.
'
).
flush
();
}
t1FinallyInterrupted.
store
(
true
);
std::cout <<
"
END t1
"
<< std::endl;
});
//
wait until t1 has set all initial values:
for
(
int
i=
0
; !t1AllSet.
load
(); ++i) {
std::this_thread::sleep_for
(10ms);
std::cout.
put
(
'
o
'
).
flush
();
}
//
and check all values:
assert
(!t0.
joinable
());
//
assert(std::stop_source{} == t0.get_stop_source());
assert
(t1.
joinable
());
assert
(t1ID == t1.
get_id
());
assert
(t1IsInterrupted ==
false
);
assert
(t1InterruptToken == t1.
get_stop_source
().
get_token
());
ssource = t1.
get_stop_source
();
assert
(t1InterruptToken.
stop_possible
());
assert
(!t1InterruptToken.
stop_requested
());
//
test detach():
t1.
detach
();
assert
(!t1.
joinable
());
}
//
leave scope of t1 without join() or detach() (signals cancellation)
//
finally signal cancellation:
assert
(!t1FinallyInterrupted.
load
());
ssource.
request_stop
();
//
and check consequences:
assert
(ssource.
stop_requested
());
for
(
int
i=
0
; !t1FinallyInterrupted.
load
() && i <
100
; ++i) {
std::this_thread::sleep_for
(100ms);
std::cout.
put
(
'
o
'
).
flush
();
}
assert
(t1FinallyInterrupted.
load
());
std::cout <<
"
\n
*** OK
"
<< std::endl;
}
//
------------------------------------------------------
void
testAssign
()
{
//
test jthread operator=()
std::cout <<
"
\n
*** start testAssign()
"
<< std::endl;
std::stop_token stoken;
{
std::jthread
t1
([](std::stop_token stoken) {
//
wait until interrupt is signaled (due to calling request_stop() for the token):
for
(
int
i=
0
; !stoken.
stop_requested
(); ++i) {
std::this_thread::sleep_for
(100ms);
std::cout.
put
(
'
.
'
).
flush
();
}
std::cout <<
"
END t1
"
<< std::endl;
});
stoken = t1.
get_stop_token
();
assert
(!stoken.
stop_requested
());
assert
(t1.
joinable
());
//
t1 is stopped and joined before being assigned:
t1 =
std::jthread
();
assert
(stoken.
stop_requested
());
assert
(!t1.
joinable
());
}
std::cout <<
"
\n
*** OK
"
<< std::endl;
}
//
------------------------------------------------------
void
testStdThread
()
{
//
test the extended std::thread API
std::cout <<
"
\n
*** start testStdThread()
"
<< std::endl;
std::thread t0;
std::thread::id t1ID{
std::this_thread::get_id
()};
std::atomic<
bool
> t1AllSet{
false
};
std::stop_source t1ShallDie;
std::thread
t1
([&t1ID, &t1AllSet, t1ShallDie = t1ShallDie.
get_token
()] {
//
check some supplementary values of the started thread:
t1ID =
std::this_thread::get_id
();
t1AllSet.
store
(
true
);
//
and wait until cancellation is signaled via passed token:
try
{
for
(
int
i=
0
; ; ++i) {
if
(t1ShallDie.
stop_requested
()) {
throw
"
interrupted
"
;
}
std::this_thread::sleep_for
(100ms);
std::cout.
put
(
'
.
'
).
flush
();
}
assert
(
false
);
}
catch
(std::exception&) {
//
interrupted not derived from std::exception
assert
(
false
);
}
catch
(
const
char
* e) {
std::cout << e << std::endl;
}
catch
(...) {
assert
(
false
);
}
assert
(t1ShallDie.
stop_requested
());
std::cout <<
"
END t1
"
<< std::endl;
});
//
wait until t1 has set all initial values:
for
(
int
i=
0
; !t1AllSet.
load
(); ++i) {
std::this_thread::sleep_for
(10ms);
std::cout.
put
(
'
o
'
).
flush
();
}
//
and check all values:
assert
(t1ID == t1.
get_id
());
//
signal cancellation via manually installed interrupt token:
t1ShallDie.
request_stop
();
t1.
join
();
std::cout <<
"
\n
*** OK
"
<< std::endl;
}
//
------------------------------------------------------
void
testTemporarilyDisableToken
()
{
//
test exchanging the token to disable it temporarily
std::cout <<
"
*** start testTemporarilyDisableToken()
"
<< std::endl;
enum
class
State
{ init, loop, disabled, restored, interrupted };
std::atomic<State> state{State::init};
std::stop_source t1is;
{
std::jthread
t1
([&state] (std::stop_token stoken) {
std::cout <<
"
- start t1
"
<< std::endl;
auto
actToken = stoken;
//
just loop (no interrupt should occur):
state.
store
(State::loop);
try
{
for
(
int
i=
0
; i <
10
; ++i) {
if
(actToken.
stop_requested
()) {
throw
"
interrupted
"
;
}
std::this_thread::sleep_for
(100ms);
std::cout.
put
(
'
.
'
).
flush
();
}
}
catch
(...) {
assert
(
false
);
}
//
temporarily disable interrupts:
std::stop_token interruptDisabled;
swap
(stoken, interruptDisabled);
state.
store
(State::disabled);
//
loop again until interrupt signaled to original interrupt token:
try
{
while
(!actToken.
stop_requested
()) {
if
(stoken.
stop_requested
()) {
throw
"
interrupted
"
;
}
std::this_thread::sleep_for
(100ms);
std::cout.
put
(
'
d
'
).
flush
();
}
for
(
int
i=
0
; i <
10
; ++i) {
std::this_thread::sleep_for
(100ms);
std::cout.
put
(
'
D
'
).
flush
();
}
}
catch
(...) {
assert
(
false
);
}
state.
store
(State::restored);
//
enable interrupts again:
swap
(stoken, interruptDisabled);
//
loop again (should immediately throw):
assert
(!interruptDisabled.
stop_requested
());
try
{
if
(actToken.
stop_requested
()) {
throw
"
interrupted
"
;
}
}
catch
(
const
char
*) {
std::cout.
put
(
'
i
'
).
flush
();
state.
store
(State::interrupted);
}
std::cout <<
"
\n
- end t1
"
<< std::endl;
});
while
(state.
load
() != State::disabled) {
std::this_thread::sleep_for
(100ms);
std::cout.
put
(
'
m
'
).
flush
();
}
std::this_thread::sleep_for
(500ms);
std::cout <<
"
\n
- leave scope (should interrupt started thread)
"
<< std::endl;
t1is = t1.
get_stop_source
();
}
//
leave scope of t1 without join() or detach() (signals cancellation)
assert
(t1is.
stop_requested
());
assert
(state.
load
() == State::interrupted);
std::cout <<
"
\n
*** OK
"
<< std::endl;
}
//
------------------------------------------------------
void
testJThreadAPI
()
{
//
test the basic jthread API (taking stop_token arg)
std::cout <<
"
*** start testJThreadAPI()
"
<< std::endl;
assert
(
std::jthread::hardware_concurrency
() ==
std::thread::hardware_concurrency
());
std::stop_source ssource;
assert
(ssource.
stop_possible
());
assert
(ssource.
get_token
().
stop_possible
());
std::stop_token stoken;
assert
(!stoken.
stop_possible
());
//
thread with no callable and invalid source:
std::jthread t0;
std::jthread::native_handle_type nh = t0.
native_handle
();
assert
((std::is_same_v<
decltype
(nh), std::thread::native_handle_type>));
assert
(!t0.
joinable
());
std::stop_source ssourceStolen{
std::move
(ssource)};
assert
(!ssource.
stop_possible
());
assert
(ssource == t0.
get_stop_source
());
assert
(ssource.
get_token
() == t0.
get_stop_token
());
{
std::jthread::id t1ID{
std::this_thread::get_id
()};
std::stop_token t1InterruptToken;
std::atomic<
bool
> t1AllSet{
false
};
std::jthread
t1
([&t1ID, &t1InterruptToken, &t1AllSet] (std::stop_token stoken) {
//
check some values of the started thread:
t1ID =
std::this_thread::get_id
();
t1InterruptToken = stoken;
assert
(stoken.
stop_possible
());
assert
(!stoken.
stop_requested
());
t1AllSet.
store
(
true
);
//
wait until interrupt is signaled (due to destructor of t1):
for
(
int
i=
0
; !stoken.
stop_requested
(); ++i) {
std::this_thread::sleep_for
(100ms);
std::cout.
put
(
'
.
'
).
flush
();
}
std::cout <<
"
END t1
"
<< std::endl;
});
//
wait until t1 has set all initial values:
for
(
int
i=
0
; !t1AllSet.
load
(); ++i) {
std::this_thread::sleep_for
(10ms);
std::cout.
put
(
'
o
'
).
flush
();
}
//
and check all values:
assert
(t1.
joinable
());
assert
(t1ID == t1.
get_id
());
assert
(t1InterruptToken == t1.
get_stop_source
().
get_token
());
assert
(t1InterruptToken == t1.
get_stop_token
());
stoken = t1.
get_stop_source
().
get_token
();
stoken = t1.
get_stop_token
();
assert
(t1InterruptToken.
stop_possible
());
assert
(!t1InterruptToken.
stop_requested
());
//
test swap():
std::swap
(t0, t1);
assert
(!t1.
joinable
());
assert
(std::stop_token{} == t1.
get_stop_source
().
get_token
());
assert
(std::stop_token{} == t1.
get_stop_token
());
assert
(t0.
joinable
());
assert
(t1ID == t0.
get_id
());
assert
(t1InterruptToken == t0.
get_stop_source
().
get_token
());
assert
(t1InterruptToken == t0.
get_stop_token
());
//
manual swap with move():
auto
ttmp{
std::move
(t0)};
t0 =
std::move
(t1);
t1 =
std::move
(ttmp);
assert
(!t0.
joinable
());
assert
(std::stop_token{} == t0.
get_stop_source
().
get_token
());
assert
(std::stop_token{} == t0.
get_stop_token
());
assert
(t1.
joinable
());
assert
(t1ID == t1.
get_id
());
assert
(t1InterruptToken == t1.
get_stop_source
().
get_token
());
assert
(t1InterruptToken == t1.
get_stop_token
());
}
//
leave scope of t1 without join() or detach() (signals cancellation)
assert
(stoken.
stop_requested
());
std::cout <<
"
\n
*** OK
"
<< std::endl;
}
//
------------------------------------------------------
//
------------------------------------------------------
int
main
()
{
std::set_terminate
([](){
std::cout <<
"
ERROR: terminate() called
"
<< std::endl;
assert
(
false
);
});
std::cout <<
"
\n\n
**************************
\n
"
;
testJThreadWithout
();
std::cout <<
"
\n\n
**************************
\n
"
;
testThreadWithToken
();
std::cout <<
"
\n\n
**************************
\n
"
;
testJoin
();
std::cout <<
"
\n\n
**************************
\n
"
;
testDetach
();
std::cout <<
"
\n\n
**************************
\n
"
;
testAssign
();
std::cout <<
"
\n\n
**************************
\n
"
;
testStdThread
();
std::cout <<
"
\n\n
**************************
\n
"
;
testTemporarilyDisableToken
();
std::cout <<
"
\n\n
**************************
\n
"
;
testJThreadAPI
();
std::cout <<
"
\n\n
**************************
\n
"
;
}
Back
|
FazBrowse Home
|
New Git URL