-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjthread.cpp
60 lines (56 loc) · 1.41 KB
/
jthread.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
#include <thread>
#include <iostream>
void f1(int& t)
{
for (int i = 0; i < 100000; ++i)
{
std::cout << "f1 running\n";
t++;
}
}
class c1
{
public:
int n = 0;
void f2()
{
for (int i = 0; i < 100000; ++i)
{
n++;
std::cout << "c1 running\n";
}
}
};
class c2
{
public:
int n = 0;
void operator()()
{
for (int i = 0; i < 100000; ++i)
{
n++;
std::cout << "c2 running\n";
}
}
};
signed main()
{
int n = 0;
c1 a;
c2 b;
{
// template< class F, class... Args > explict jthread(F&& f, Args&&... args)
std::jthread t1(f1, std::ref(n));
// jthread(jthread&& other) noexcept
std::jthread t2(std::move(t1));
// jthread() noexcept
std::jthread t3;
// template< class F, class... Args > explict jthread(F&& f, Args&&... args)
std::jthread t4(&c1::f2, &a);
// template< class F, class... Args > explict jthread(F&& f, Args&&... args)
std::jthread t5(b);
}
std::cout << n << " " << a.n << " " << b.n << "\n";
return 0;
}