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 | #define BOOST_THREAD_VERSION 3 |
8 | |
9 | #include <iostream> |
10 | #include <boost/thread/scoped_thread.hpp> |
11 | |
12 | void do_something(int& i) |
13 | { |
14 | ++i; |
15 | } |
16 | void f(int, int) |
17 | { |
18 | } |
19 | |
20 | struct func |
21 | { |
22 | int& i; |
23 | |
24 | func(int& i_) : |
25 | i(i_) |
26 | { |
27 | } |
28 | |
29 | void operator()() |
30 | { |
31 | for (unsigned j = 0; j < 1000000; ++j) |
32 | { |
33 | do_something(i); |
34 | } |
35 | } |
36 | }; |
37 | |
38 | void do_something_in_current_thread() |
39 | { |
40 | } |
41 | |
42 | int main() |
43 | { |
44 | { |
45 | int some_local_state=0; |
46 | boost::strict_scoped_thread<> t( (boost::thread(func(some_local_state)))); |
47 | |
48 | do_something_in_current_thread(); |
49 | } |
50 | { |
51 | int some_local_state=0; |
52 | boost::thread t(( func(some_local_state) )); |
53 | boost::strict_scoped_thread<> g( (boost::move(t)) ); |
54 | |
55 | do_something_in_current_thread(); |
56 | } |
57 | { |
58 | int some_local_state=0; |
59 | boost::scoped_thread<> t( (boost::thread(func(some_local_state)))); |
60 | |
61 | if (t.joinable()) |
62 | t.join(); |
63 | else |
64 | do_something_in_current_thread(); |
65 | } |
66 | #if 0 |
67 | { |
68 | int some_local_state=0; |
69 | boost::thread t(( func(some_local_state) )); |
70 | boost::scoped_thread<> g( (boost::move(t)) ); |
71 | if (g.joinable()) |
72 | g.detach(); |
73 | |
74 | do_something_in_current_thread(); |
75 | } |
76 | #endif |
77 | { |
78 | boost::scoped_thread<> g( &f, 1, 2 ); |
79 | do_something_in_current_thread(); |
80 | } |
81 | return 0; |
82 | } |
83 | |
84 |