-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.cpp
65 lines (58 loc) · 1.39 KB
/
thread.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
#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 thread(F&& f, Args&&... args)
std::thread t1(f1, std::ref(n));
// thread(thread&& other) noexcept
std::thread t2(std::move(t1));
// thread() noexcept
std::thread t3;
// template< class F, class... Args > explict thread(F&& f, Args&&... args)
std::thread t4(&c1::f2, &a);
// template< class F, class... Args > explict thread(F&& f, Args&&... args)
std::thread t5(b);
t2.join();
t4.join();
t5.join();
// t3.join(); error!
std::cout << n << " " << a.n << " " << b.n << "\n";
return 0;
}