]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
7c673cae FG |
42 | int main() |
43 | { | |
44 | { | |
b32b8144 | 45 | int some_local_state=0; |
7c673cae FG |
46 | boost::strict_scoped_thread<> t( (boost::thread(func(some_local_state)))); |
47 | ||
48 | do_something_in_current_thread(); | |
49 | } | |
50 | { | |
b32b8144 | 51 | int some_local_state=0; |
7c673cae FG |
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 | } | |
7c673cae | 57 | { |
b32b8144 | 58 | int some_local_state=0; |
7c673cae FG |
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 | } | |
b32b8144 | 66 | #if 0 |
7c673cae | 67 | { |
b32b8144 | 68 | int some_local_state=0; |
7c673cae FG |
69 | boost::thread t(( func(some_local_state) )); |
70 | boost::scoped_thread<> g( (boost::move(t)) ); | |
b32b8144 FG |
71 | if (g.joinable()) |
72 | g.detach(); | |
7c673cae FG |
73 | |
74 | do_something_in_current_thread(); | |
75 | } | |
b32b8144 | 76 | #endif |
7c673cae FG |
77 | { |
78 | boost::scoped_thread<> g( &f, 1, 2 ); | |
79 | do_something_in_current_thread(); | |
80 | } | |
81 | return 0; | |
82 | } | |
83 |