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 main()8int main() 9 { 10 return 0; 11 } 12 #else 13 14 #define BOOST_THREAD_VERSION 3 15 16 #include <iostream> 17 #include <boost/thread/scoped_thread.hpp> 18 #include <thread> 19 #include <cassert> 20 do_something(int & i)21void do_something(int& i) 22 { 23 ++i; 24 } f(int,int)25void f(int, int) 26 { 27 } 28 29 struct func 30 { 31 int& i; 32 funcfunc33 func(int& i_) : 34 i(i_) 35 { 36 } 37 operator ()func38 void operator()() 39 { 40 for (unsigned j = 0; j < 1000000; ++j) 41 { 42 do_something(i); 43 } 44 } 45 }; 46 do_something_in_current_thread()47void do_something_in_current_thread() 48 { 49 } 50 51 using strict_scoped_thread = boost::strict_scoped_thread<boost::join_if_joinable, std::thread>; 52 using scoped_thread = boost::scoped_thread<boost::join_if_joinable, std::thread>; 53 main()54int main() 55 { 56 { 57 int some_local_state=0; 58 strict_scoped_thread t( (std::thread(func(some_local_state)))); 59 60 do_something_in_current_thread(); 61 } 62 { 63 int some_local_state=0; 64 std::thread t(( func(some_local_state) )); 65 strict_scoped_thread g( (boost::move(t)) ); 66 67 do_something_in_current_thread(); 68 } 69 { 70 int some_local_state=0; 71 std::thread t(( func(some_local_state) )); 72 strict_scoped_thread g( (std::move(t)) ); 73 74 do_something_in_current_thread(); 75 } 76 { 77 int some_local_state=1; 78 scoped_thread t( (std::thread(func(some_local_state)))); 79 80 if (t.joinable()) { 81 t.join(); 82 assert( ! t.joinable() ); 83 } 84 else 85 do_something_in_current_thread(); 86 } 87 #if 0 88 try 89 { 90 int some_local_state=1; 91 std::thread t(( func(some_local_state) )); 92 scoped_thread g( (boost::move(t)) ); 93 if (g.joinable()) { 94 // CLANG crash here 95 g.detach(); 96 assert( ! g.joinable() ); 97 } 98 99 do_something_in_current_thread(); 100 } 101 catch (...) { 102 assert( false); 103 } 104 #endif 105 { 106 scoped_thread g( &f, 1, 2 ); 107 do_something_in_current_thread(); 108 } 109 return 0; 110 } 111 112 #endif 113