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 | |
8 | #include <iostream> |
9 | #include <string> |
10 | #include <boost/thread/thread_only.hpp> |
11 | #include <boost/thread/thread_guard.hpp> |
12 | |
13 | void do_something(int& i) |
14 | { |
15 | ++i; |
16 | } |
17 | |
18 | struct func |
19 | { |
20 | int& i; |
21 | |
22 | func(int& i_):i(i_){} |
23 | func(func const& other):i(other.i){} |
24 | |
25 | void operator()() |
26 | { |
27 | for(unsigned j=0;j<1000000;++j) |
28 | { |
29 | do_something(i); |
30 | } |
31 | } |
32 | |
33 | private: |
34 | func& operator=(func const&); |
35 | |
36 | }; |
37 | |
38 | void do_something_in_current_thread() |
39 | {} |
40 | |
41 | |
42 | void f() |
43 | { |
44 | int some_local_state; |
45 | func my_func(some_local_state); |
46 | boost::thread t(my_func); |
47 | boost::thread_guard<> g(t); |
48 | |
49 | do_something_in_current_thread(); |
50 | } |
51 | |
52 | int main() |
53 | { |
54 | f(); |
55 | return 0; |
56 | } |
57 | |
58 | |
59 |