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 4
22 #define BOOST_THREAD_PROVIDES_EXECUTORS
23 #include <boost/config.hpp>
24 #if ! defined BOOST_NO_CXX11_DECLTYPE
25 #define BOOST_RESULT_OF_USE_DECLTYPE
28 #include <boost/thread/future.hpp>
29 #include <boost/thread/thread.hpp>
30 #include <boost/thread/detail/memory.hpp>
31 #include <boost/thread/csbl/memory/unique_ptr.hpp>
33 #include <boost/detail/lightweight_test.hpp>
34 #include <boost/thread/executors/basic_thread_pool.hpp>
35 #include <boost/thread/executor.hpp>
37 typedef boost::chrono::high_resolution_clock Clock
;
38 typedef boost::chrono::milliseconds ms
;
45 typedef long result_type
;
54 boost::this_thread::sleep_for(ms(200));
57 long operator()() const
59 boost::this_thread::sleep_for(ms(200));
67 typedef int result_type
;
71 BOOST_THREAD_MOVABLE_ONLY(MoveOnly
)
76 MoveOnly( BOOST_THREAD_RV_REF(MoveOnly
))
80 MoveOnly
& operator=(BOOST_THREAD_RV_REF(MoveOnly
))
88 boost::this_thread::sleep_for(ms(200));
91 template <typename OS
>
92 friend OS
& operator<<(OS
& os
, MoveOnly
const& v
)
101 BOOST_THREAD_DCL_MOVABLE (MoveOnly
)
106 boost::this_thread::sleep_for(ms(200));
114 boost::this_thread::sleep_for(ms(200));
120 boost::this_thread::sleep_for(ms(200));
123 boost::csbl::unique_ptr
<int> f3_0()
125 boost::this_thread::sleep_for(ms(200));
126 boost::csbl::unique_ptr
<int> r( (new int(3)));
127 return boost::move(r
);
131 boost::this_thread::sleep_for(ms(200));
133 return boost::move(r
);
136 boost::csbl::unique_ptr
<int> f3(int i
)
138 boost::this_thread::sleep_for(ms(200));
139 return boost::csbl::unique_ptr
<int>(new int(i
));
142 boost::csbl::unique_ptr
<int> f4(
143 BOOST_THREAD_RV_REF_BEG
boost::csbl::unique_ptr
<int> BOOST_THREAD_RV_REF_END p
146 boost::this_thread::sleep_for(ms(200));
147 return boost::move(p
);
151 boost::chrono::nanoseconds delay
;
152 Clock::time_point start
;
153 check_timer(boost::chrono::nanoseconds delay
)
155 , start(Clock::now())
159 Clock::time_point now
= Clock::now();
160 BOOST_TEST(now
- start
< delay
);
161 std::cout
<< __FILE__
<< "[" << __LINE__
<< "] " << (now
- start
).count() << std::endl
;
168 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
169 #if defined BOOST_THREAD_PROVIDES_EXECUTORS
173 boost::executor_adaptor
<boost::basic_thread_pool
> ex(1);
174 boost::future
<int> f
= boost::async(ex
, &f0
);
175 boost::this_thread::sleep_for(ms(300));
178 check_timer
timer(ms(500));
181 BOOST_TEST(res
== 3);
183 catch (std::exception
& ex
)
185 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
186 BOOST_TEST(false && "exception thrown");
190 BOOST_TEST(false && "exception thrown");
194 #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) && defined BOOST_THREAD_PROVIDES_EXECUTORS
195 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
199 boost::executor_adaptor
<boost::basic_thread_pool
> ex(1);
200 boost::future
<long> f
= boost::async(ex
, A(3));
201 boost::this_thread::sleep_for(ms(300));
204 check_timer
timer(ms(500));
207 BOOST_TEST(res
== 3);
209 catch (std::exception
& ex
)
211 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
212 BOOST_TEST(false && "exception thrown");
216 BOOST_TEST(false && "exception thrown");
221 #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) && defined BOOST_THREAD_PROVIDES_EXECUTORS
222 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << std::endl
;
226 boost::executor_adaptor
<boost::basic_thread_pool
> ex(1);
228 boost::future
<int> f
= boost::async(ex
, boost::move(mo
));
229 //boost::future<int> f = boost::async(ex, MoveOnly());
230 boost::this_thread::sleep_for(ms(300));
233 check_timer
timer(ms(500));
236 BOOST_TEST(res
== 3);
238 catch (std::exception
& ex
)
240 std::cout
<< __FILE__
<< "[" << __LINE__
<< "]" << ex
.what() << std::endl
;
241 BOOST_TEST(false && "exception thrown");
245 BOOST_TEST(false && "exception thrown");
250 return boost::report_errors();