1 // Copyright (C) 2012 Vicente Botet
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/config.hpp>
7 #if ! defined BOOST_NO_CXX11_DECLTYPE
8 #define BOOST_RESULT_OF_USE_DECLTYPE
11 #define BOOST_THREAD_VERSION 4
13 #include <boost/thread/future.hpp>
20 exception_ptr
make_exception_ptr(T v
)
22 return copy_exception(v
);
26 int p1() { return 5; }
27 int& p1r() { static int i
=0; return i
; }
31 #if defined BOOST_THREAD_USES_MOVE
32 boost::future
<void> void_compute()
34 return BOOST_THREAD_MAKE_RV_REF(boost::make_ready_future());
38 boost::future
<int> compute(int x
)
40 if (x
== 0) return boost::make_ready_future(0);
41 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
42 if (x
< 0) return boost::make_exceptional_future
<int>(std::logic_error("Error"));
44 if (x
< 0) return boost::make_exceptional(std::logic_error("Error"));
46 //boost::future<int> f1 = boost::async([]() { return x+1; });
47 boost::future
<int> f1
= boost::async(p1
);
48 return boost::move(f1
);
51 boost::future
<int&> compute_ref(int x
)
54 //if (x == 0) return boost::make_ready_future<int&>(i); //This must not compile as the type is deduced as boost::future<int>
55 if (x
== 0) return boost::make_ready_no_decay_future
<int&>(i
);
56 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
57 if (x
< 0) return boost::make_exceptional_future
<int&>(std::logic_error("Error"));
59 if (x
< 0) return boost::make_exceptional(std::logic_error("Error"));
61 boost::future
<int&> f1
= boost::async(p1r
);
62 return boost::move(f1
);
65 boost::shared_future
<int> shared_compute(int x
)
67 if (x
== 0) return boost::make_ready_future(0).share();
68 #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
69 if (x
< 0) return boost::make_exceptional_future
<int>(std::logic_error("Error")).share();
71 if (x
< 0) return boost::make_exceptional(std::logic_error("Error"));
73 //boost::future<int> f1 = boost::async([]() { return x+1; });
74 boost::shared_future
<int> f1
= boost::async(&p1
).share();
81 const int number_of_tests
= 100;
82 for (int i
=0; i
< number_of_tests
; i
++)
86 // std::cout << __FILE__ << " "<<__LINE__ << std::endl;
87 // boost::future<int> f = boost::async(boost::launch::async, p1);
88 // std::cout << i << " "<<f.get() << std::endl;
90 #if defined BOOST_THREAD_USES_MOVE
92 std::cout
<< __FILE__
<< " "<< __LINE__
<< std::endl
;
93 boost::future
<void> f
= void_compute();
98 std::cout
<< __FILE__
<< " "<< __LINE__
<< std::endl
;
99 boost::future
<int> f
= compute(-1);
103 std::cout
<< __FILE__
<< " "<< __LINE__
<< std::endl
;
104 boost::future
<int> f
= compute(0);
105 std::cout
<< f
.get() << std::endl
;
108 std::cout
<< __FILE__
<< " "<< __LINE__
<< std::endl
;
109 boost::future
<int&> f
= compute_ref(0);
110 std::cout
<< f
.get() << std::endl
;
112 #if __cplusplus > 201103L
114 std::cout
<< __FILE__
<< " "<< __LINE__
<< std::endl
;
116 boost::future
<int&> f
= boost::make_ready_future(std::ref(i
));
117 std::cout
<< f
.get() << std::endl
;
121 std::cout
<< __FILE__
<< " "<< __LINE__
<< std::endl
;
123 boost::future
<int&> f
= boost::make_ready_future(boost::ref(i
));
124 std::cout
<< f
.get() << std::endl
;
127 std::cout
<< __FILE__
<< " "<< __LINE__
<< std::endl
;
129 boost::future
<int const&> f
= boost::make_ready_future(boost::cref(i
));
130 std::cout
<< f
.get() << std::endl
;
133 std::cout
<< __FILE__
<< " "<< __LINE__
<< std::endl
;
134 boost::future
<int> f
= compute(2);
135 std::cout
<< f
.get() << std::endl
;
138 std::cout
<< __FILE__
<< " "<< __LINE__
<< std::endl
;
139 boost::shared_future
<int> f
= shared_compute(0);
140 std::cout
<< f
.get() << std::endl
;
143 catch (std::exception
& ex
)
145 std::cout
<< "ERRORRRRR "<<ex
.what() << "" << std::endl
;
150 std::cout
<< "ERRORRRRR "<<"ERRORRRRR exception thrown" << std::endl
;