-
Notifications
You must be signed in to change notification settings - Fork 2
/
jthread.t.cpp
142 lines (104 loc) · 3.06 KB
/
jthread.t.cpp
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
#include "jthread-main.t.hpp"
namespace {
CASE("jthread: Allows to default-construct a jthread")
{
nonstd::jthread thr;
EXPECT( thr.get_id() != std::this_thread::get_id() );
}
CASE("jthread: Allows to create a thread with a callback - no parameters")
{
int gx = 7;
{
nonstd::jthread thr{ [&]{ gx = 42; } };
EXPECT( thr.joinable() );
}
EXPECT( gx == 42 );
}
CASE("jthread: Allows to create a thread with a callback - with parameters")
{
int gx = 7;
{
nonstd::jthread thr{ [&](int x, int y){ gx = x * y; }, 6, 7 };
EXPECT( thr.joinable() );
}
EXPECT( gx == 42 );
}
CASE("jthread: Allows to check if a thread is joinable")
{
nonstd::jthread thr{ []{} };
EXPECT( thr.joinable() );
}
CASE("jthread: Allows to join a thread")
{
int gx = 7;
nonstd::jthread thr{ [&](int x, int y){ gx = x * y; }, 6, 7 };
thr.join();
EXPECT( gx == 42 );
}
CASE("jthread: Allows to detach a thread")
{
nonstd::jthread thr{ []{} };
thr.detach();
EXPECT_NOT( thr.joinable() );
}
CASE("jthread: Allows to obtain stop_source - stop_source not-implemented")
{
nonstd::jthread thr{ []{} };
nonstd::stop_source stop = thr.get_stop_source();
}
CASE("jthread: Allows to obtain stop_token - stop_token not-implemented")
{
nonstd::jthread thr{ []{} };
nonstd::stop_source ssrc = thr.get_stop_source();
}
CASE("jthread: Allows to request a thread to stop - stop_token not-implemented")
{
nonstd::jthread thr{ []{} };
// thr.request_stop();
}
CASE("jthread: Allows to swap two threads")
{
nonstd::jthread t1{ []{} };
nonstd::jthread t2{ []{} };
nonstd::jthread::id tid1 = t1.get_id();
nonstd::jthread::id tid2 = t2.get_id();
swap( t1, t2 );
EXPECT( t1.get_id() == tid2 );
EXPECT( t2.get_id() == tid1 );
}
CASE("jthread: Allows to obtain a thread's id" "[.jthread][.info]")
{
nonstd::jthread thr{ []{} };
nonstd::jthread::id tid = thr.get_id();
std::cout << "Info: get_id(): " << tid << "\n";
}
CASE("jthread: Allows to obtain a thread's native handle" "[.jthread][.info]")
{
nonstd::jthread thr{ []{} };
nonstd::jthread::native_handle_type tnh = thr.native_handle();
std::cout << "Info: native_handle(): " << tnh << "\n";
}
CASE("jthread: Allows to obtain the maximum number of hardware threads" "[.jthread][.info]")
{
std::cout << "Info: hardware_concurrency(): " << nonstd::jthread::hardware_concurrency() << "\n";
}
CASE("jthread: App" "[.jthread][.app]")
{
int product = 0;
const int six = 6;
const int seven = 7;
{
nonstd::jthread thr{[&](int x, int y){ product = x * y; }, six, seven };
// automatically join thread here, making sure it has executed:
}
std::cout << "App: Product of "<< six << " and " << seven << " is " << product << ".\n";
}
CASE( "tweak header: Reads tweak header if supported " "[tweak]" )
{
#if jthread_HAVE_TWEAK_HEADER
EXPECT( jthread_TWEAK_VALUE == 42 );
#else
EXPECT( !!"Tweak header is not available (jthread_HAVE_TWEAK_HEADER: 0)." );
#endif
}
} // anonymous namespace