1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Copyright (C) 2011 Vicente J. Botet Escriba
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)
15 // <boost/thread/future.hpp>
17 // template <class Executor, class F, class... Args>
18 // future<typename result_of<F(Args...)>::type>
19 // async(Executor& ex, F&& f, Args&&... args);
21 #define BOOST_THREAD_VERSION 5
22 #include <boost/config.hpp>
23 #if ! defined BOOST_NO_CXX11_DECLTYPE
24 #define BOOST_RESULT_OF_USE_DECLTYPE
27 #include <boost/thread/future.hpp>
28 #include <boost/thread/thread.hpp>
29 #include <boost/thread/detail/memory.hpp>
30 #include <boost/thread/csbl/memory/unique_ptr.hpp>
32 #include <boost/detail/lightweight_test.hpp>
33 #include <boost/thread/executors/basic_thread_pool.hpp>
34 #include <boost/thread/executor.hpp>
36 typedef boost::chrono::high_resolution_clock Clock
;
37 typedef boost::chrono::milliseconds ms
;
44 typedef long result_type
;
53 boost::this_thread::sleep_for(ms(200));
56 long operator()() const
58 boost::this_thread::sleep_for(ms(200));
66 typedef int result_type
;
70 BOOST_THREAD_MOVABLE_ONLY(MoveOnly
)
75 MoveOnly( BOOST_THREAD_RV_REF(MoveOnly
))
79 MoveOnly
& operator=(BOOST_THREAD_RV_REF(MoveOnly
))
87 boost::this_thread::sleep_for(ms(200));
90 template <typename OS
>
91 friend OS
& operator<<(OS
& os
, MoveOnly
const& v
)
100 BOOST_THREAD_DCL_MOVABLE (MoveOnly
)
105 boost::this_thread::sleep_for(ms(200));
113 boost::this_thread::sleep_for(ms(200));
119 boost::this_thread::sleep_for(ms(200));
122 boost::csbl::unique_ptr
<int> f3_0()
124 boost::this_thread::sleep_for(ms(200));
125 boost::csbl::unique_ptr
<int> r( (new int(3)));
126 return boost::move(r
);
130 boost::this_thread::sleep_for(ms(200));
132 return boost::move(r
);
135 boost::csbl::unique_ptr
<int> f3(int i
)
137 boost::this_thread::sleep_for(ms(200));
138 return boost::csbl::unique_ptr
<int>(new int(i
));
141 boost::csbl::unique_ptr
<int> f4(
142 BOOST_THREAD_RV_REF_BEG
boost::csbl::unique_ptr
<int> BOOST_THREAD_RV_REF_END p
145 boost::this_thread::sleep_for(ms(200));
146 return boost::move(p
);
150 boost::chrono::nanoseconds delay
;
151 Clock::time_point start
;
152 check_timer(boost::chrono::nanoseconds delay
)
154 , start(Clock::now())
158 Clock::time_point now
= Clock::now();
159 BOOST_TEST(now
- start
< delay
);
160 std::cout
<< __FILE__
<< "[" << __LINE__
<< "] " << (now
- start
).count() << std::endl
;
167 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
168 #if defined BOOST_THREAD_PROVIDES_EXECUTORS
172 boost::executor_adaptor
<boost::basic_thread_pool
> ex(1);
173 boost::future
<int> f
= boost::async(ex
, &f0
);
174 boost::this_thread::sleep_for(ms(300));
177 check_timer
timer(ms(500));
180 BOOST_TEST(res
== 3);
182 catch (std::exception
& ex
)
184 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
185 BOOST_TEST(false && "exception thrown");
189 BOOST_TEST(false && "exception thrown");
193 #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) && defined BOOST_THREAD_PROVIDES_EXECUTORS
194 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
198 boost::executor_adaptor
<boost::basic_thread_pool
> ex(1);
199 boost::future
<long> f
= boost::async(ex
, A(3));
200 boost::this_thread::sleep_for(ms(300));
203 check_timer
timer(ms(500));
206 BOOST_TEST(res
== 3);
208 catch (std::exception
& ex
)
210 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
211 BOOST_TEST(false && "exception thrown");
215 BOOST_TEST(false && "exception thrown");
220 #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) && defined BOOST_THREAD_PROVIDES_EXECUTORS
221 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
225 boost::executor_adaptor
<boost::basic_thread_pool
> ex(1);
227 boost::future
<int> f
= boost::async(ex
, boost::move(mo
));
228 //boost::future<int> f = boost::async(ex, MoveOnly());
229 boost::this_thread::sleep_for(ms(300));
232 check_timer
timer(ms(500));
235 BOOST_TEST(res
== 3);
237 catch (std::exception
& ex
)
239 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
240 BOOST_TEST(false && "exception thrown");
244 BOOST_TEST(false && "exception thrown");
249 return boost::report_errors();