1 | // (C) Copyright 2009-2012 Anthony Williams |
---|---|
2 | // (C) Copyright 2012 Vicente Botet |
3 | // |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
6 | |
7 | #if __cplusplus < 201103L |
8 | int main() |
9 | { |
10 | return 0; |
11 | } |
12 | #else |
13 | #include <iostream> |
14 | #include <string> |
15 | #include <boost/thread/thread_only.hpp> |
16 | #include <boost/thread/thread_guard.hpp> |
17 | #include <thread> |
18 | |
19 | void do_something(int& i) |
20 | { |
21 | ++i; |
22 | } |
23 | |
24 | struct func |
25 | { |
26 | int& i; |
27 | |
28 | func(int& i_):i(i_){} |
29 | |
30 | void operator()() |
31 | { |
32 | for(unsigned j=0;j<1000000;++j) |
33 | { |
34 | do_something(i); |
35 | } |
36 | } |
37 | |
38 | private: |
39 | func& operator=(func const&); |
40 | |
41 | }; |
42 | |
43 | void do_something_in_current_thread() |
44 | {} |
45 | |
46 | using thread_guard = boost::thread_guard<boost::join_if_joinable, std::thread>; |
47 | |
48 | |
49 | void f() |
50 | { |
51 | int some_local_state; |
52 | func my_func(some_local_state); |
53 | std::thread t(my_func); |
54 | thread_guard g(t); |
55 | |
56 | do_something_in_current_thread(); |
57 | } |
58 | |
59 | int main() |
60 | { |
61 | f(); |
62 | return 0; |
63 | } |
64 | |
65 | |
66 | #endif |
67 |