-
Notifications
You must be signed in to change notification settings - Fork 0
/
listing_2.6.cpp
64 lines (55 loc) · 1.06 KB
/
listing_2.6.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
#include <thread>
#include <utility>
#include <iostream>
#include <stdexcept>
class scoped_thread
{
std::thread t;
public:
explicit scoped_thread(std::thread t_):
t(std::move(t_))
{
std::cout << "ctr" << std::endl;
if(!t.joinable()) {
std::cout << "error" << std::endl;
throw std::out_of_range("No thread");
}
}
~scoped_thread()
{
std::cout << "join" << std::endl;
std::flush(std::cout);
t.join();
}
scoped_thread(scoped_thread const&)=delete;
scoped_thread& operator=(scoped_thread const&)=delete;
};
void do_something(int& i)
{
++i;
}
struct func
{
int& i;
func(int& i_):i(i_){}
void operator()()
{
for(unsigned j=0;j<1000000;++j)
{
do_something(i);
}
std::cout << "run once" << std::endl;
}
};
void do_something_in_current_thread()
{}
void f()
{
int some_local_state;
scoped_thread t(std::thread(func(some_local_state)));
do_something_in_current_thread();
}
int main()
{
f();
}