1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // Copyright (C) 2011 Vicente J. Botet Escriba 11 // 12 // Distributed under the Boost Software License, Version 1.0. (See accompanying 13 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 14 15 // <boost/thread/future.hpp> 16 17 // class promise<R> 18 19 // void promise<void>::set_value_at_thread_exit(); 20 21 #define BOOST_THREAD_VERSION 4 22 23 #include <boost/thread/future.hpp> 24 #include <boost/detail/lightweight_test.hpp> 25 26 int i = 0; 27 28 boost::promise<void> p; func()29void func() 30 { 31 p.set_value_at_thread_exit(); 32 i = 1; 33 } 34 35 //void func2_mv(BOOST_THREAD_RV_REF(boost::promise<void>) p2) func2_mv(boost::promise<void> p2)36void func2_mv(boost::promise<void> p2) 37 { 38 p2.set_value_at_thread_exit(); 39 i = 2; 40 } 41 func2(boost::promise<void> * p2)42void func2(boost::promise<void> *p2) 43 { 44 p2->set_value_at_thread_exit(); 45 i = 2; 46 } main()47int main() 48 { 49 try 50 { 51 boost::future<void> f = p.get_future(); 52 boost::thread(func).detach(); 53 f.get(); 54 BOOST_TEST(i == 1); 55 56 } 57 catch(std::exception& ) 58 { 59 BOOST_TEST(false); 60 } 61 catch(...) 62 { 63 BOOST_TEST(false); 64 } 65 66 try 67 { 68 boost::promise<void> p2; 69 boost::future<void> f = p2.get_future(); 70 p = boost::move(p2); 71 boost::thread(func).detach(); 72 f.get(); 73 BOOST_TEST(i == 1); 74 75 } 76 catch(std::exception& ex) 77 { 78 std::cout << __FILE__ << ":" << __LINE__ << " " << ex.what() << std::endl; 79 BOOST_TEST(false); 80 } 81 catch(...) 82 { 83 BOOST_TEST(false); 84 } 85 86 try 87 { 88 boost::promise<void> p2; 89 boost::future<void> f = p2.get_future(); 90 #if defined BOOST_THREAD_PROVIDES_VARIADIC_THREAD 91 boost::thread(func2_mv, boost::move(p2)).detach(); 92 #else 93 boost::thread(func2, &p2).detach(); 94 #endif 95 f.wait(); 96 f.get(); 97 BOOST_TEST(i == 2); 98 } 99 catch(std::exception& ex) 100 { 101 std::cout << __FILE__ << ":" << __LINE__ << " " << ex.what() << std::endl; 102 BOOST_TEST(false); 103 } 104 catch(...) 105 { 106 BOOST_TEST(false); 107 } 108 return boost::report_errors(); 109 } 110 111