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::set_value(const R& r); 20 21 #define BOOST_THREAD_VERSION 3 22 23 #include <boost/thread/future.hpp> 24 #include <boost/detail/lightweight_test.hpp> 25 #include <boost/static_assert.hpp> 26 #include <boost/config.hpp> 27 28 #ifdef BOOST_MSVC 29 # pragma warning(disable: 4702) // unreachable code 30 #endif 31 32 struct A 33 { AA34 A() 35 { 36 } AA37 A(const A&) 38 { 39 throw 10; 40 } 41 #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) 42 A& operator= (const A&) = default; 43 #endif 44 }; 45 main()46int main() 47 { 48 49 { 50 typedef int T; 51 T i = 3; 52 boost::promise<T> p; 53 boost::future<T> f = p.get_future(); 54 p.set_value(i); 55 ++i; 56 BOOST_TEST(f.get() == 3); 57 --i; 58 try 59 { 60 p.set_value(i); 61 BOOST_TEST(false); 62 } 63 catch (const boost::future_error& e) 64 { 65 BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::promise_already_satisfied)); 66 } 67 catch (...) 68 { 69 BOOST_TEST(false); 70 } 71 } 72 { 73 typedef int T; 74 T i = 3; 75 boost::promise<T> p; 76 boost::future<T> f = p.get_future(); 77 p.set_value_deferred(i); 78 p.notify_deferred(); 79 ++i; 80 BOOST_TEST(f.get() == 3); 81 --i; 82 try 83 { 84 p.set_value(i); 85 BOOST_TEST(false); 86 } 87 catch (const boost::future_error& e) 88 { 89 BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::promise_already_satisfied)); 90 } 91 catch (...) 92 { 93 BOOST_TEST(false); 94 } 95 } 96 { 97 typedef A T; 98 T i; 99 boost::promise<T> p; 100 boost::future<T> f = p.get_future(); 101 try 102 { 103 p.set_value(i); 104 BOOST_TEST(false); 105 } 106 catch (int j) 107 { 108 BOOST_TEST(j == 10); 109 } 110 catch (...) 111 { 112 BOOST_TEST(false); 113 } 114 } 115 116 return boost::report_errors(); 117 } 118 119