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) 2013 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 shared_future<R> 18 19 // template <class Rep, class Period> 20 // void wait() const; 21 22 //#define BOOST_THREAD_VERSION 3 23 #define BOOST_THREAD_VERSION 4 24 //#define BOOST_THREAD_USES_LOG 25 #define BOOST_THREAD_USES_LOG_THREAD_ID 26 #include <boost/thread/detail/log.hpp> 27 28 #include <boost/thread/future.hpp> 29 #include <boost/thread/thread.hpp> 30 #include <boost/chrono/chrono_io.hpp> 31 #include <boost/detail/lightweight_test.hpp> 32 33 #if defined BOOST_THREAD_USES_CHRONO 34 35 #ifdef BOOST_MSVC 36 #pragma warning(disable: 4127) // conditional expression is constant 37 #endif 38 39 typedef boost::chrono::milliseconds ms; 40 41 namespace boost 42 { 43 template <typename OStream> operator <<(OStream & os,boost::future_status st)44 OStream& operator<<(OStream& os , boost::future_status st ) 45 { 46 os << underlying_cast<int>(st) << " "; 47 return os; 48 } 49 template <typename T> 50 struct wrap 51 { wrapboost::wrap52 wrap(T const& v) : 53 value(v) 54 { 55 } 56 T value; 57 58 }; 59 60 template <typename T> make_exception_ptr(T v)61 exception_ptr make_exception_ptr(T v) 62 { 63 return copy_exception(wrap<T> (v)); 64 } 65 } 66 func1(boost::promise<int> p)67void func1(boost::promise<int> p) 68 { 69 boost::this_thread::sleep_for(ms(500)); 70 p.set_value(3); 71 } 72 73 int j = 0; 74 func3(boost::promise<int &> p)75void func3(boost::promise<int&> p) 76 { 77 boost::this_thread::sleep_for(ms(500)); 78 j = 5; 79 p.set_value(j); 80 } 81 func5(boost::promise<void> p)82void func5(boost::promise<void> p) 83 { 84 boost::this_thread::sleep_for(ms(500)); 85 p.set_value(); 86 } 87 main()88int main() 89 { 90 BOOST_THREAD_LOG << BOOST_THREAD_END_LOG; 91 { 92 typedef boost::chrono::high_resolution_clock Clock; 93 { 94 typedef int T; 95 boost::promise<T> p; 96 boost::shared_future<T> f((p.get_future())); 97 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) 98 boost::thread(func1, boost::move(p)).detach(); 99 #else 100 func1(boost::move(p)); 101 #endif 102 BOOST_TEST(f.valid()); 103 f.wait(); 104 BOOST_TEST(f.valid()); 105 Clock::time_point t0 = Clock::now(); 106 f.wait(); 107 Clock::time_point t1 = Clock::now(); 108 BOOST_TEST(f.valid()); 109 BOOST_TEST(t1 - t0 < ms(50)); 110 } 111 { 112 typedef int& T; 113 boost::promise<T> p; 114 boost::shared_future<T> f((p.get_future())); 115 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) 116 boost::thread(func3, boost::move(p)).detach(); 117 #else 118 func3(boost::move(p)); 119 #endif 120 BOOST_TEST(f.valid()); 121 f.wait(); 122 BOOST_TEST(f.valid()); 123 Clock::time_point t0 = Clock::now(); 124 f.wait(); 125 Clock::time_point t1 = Clock::now(); 126 BOOST_TEST(f.valid()); 127 BOOST_TEST(t1 - t0 < ms(50)); 128 } 129 { 130 typedef void T; 131 boost::promise<T> p; 132 boost::shared_future<T> f((p.get_future())); 133 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) 134 boost::thread(func5, boost::move(p)).detach(); 135 #else 136 func5(boost::move(p)); 137 #endif 138 BOOST_TEST(f.valid()); 139 f.wait(); 140 BOOST_TEST(f.valid()); 141 Clock::time_point t0 = Clock::now(); 142 f.wait(); 143 Clock::time_point t1 = Clock::now(); 144 BOOST_TEST(f.valid()); 145 BOOST_TEST(t1 - t0 < ms(50)); 146 } 147 } 148 BOOST_THREAD_LOG << BOOST_THREAD_END_LOG; 149 150 return boost::report_errors(); 151 } 152 153 #else 154 #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported" 155 #endif 156