1 // Copyright (C) 2010 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 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
9 #include <boost/detail/lightweight_test.hpp>
10 #include <boost/thread/future.hpp>
11 #include <boost/utility/result_of.hpp>
15 virtual ~async_func() { }
16 virtual void run() =0;
19 template <typename Ret
>
20 class async_func_pt
: public async_func
{
21 boost::packaged_task
<Ret
> f
;
24 std::cout
<< __FILE__
<< ":" << __LINE__
<< std::endl
;
26 async_func_pt (boost::packaged_task
<Ret
>&& f
) : f(boost::move(f
)) {}
28 boost::unique_future
<Ret
> get_future() { return f
.get_future(); }
31 void async_core (async_func
* p
) {
32 std::cout
<< __FILE__
<< ":" << __LINE__
<< std::endl
;
40 boost::unique_future
<typename
boost::result_of
< F() >::type
>
43 std::cout
<< __FILE__
<< ":" << __LINE__
<< std::endl
;
44 typedef typename
boost::result_of
< F() >::type RetType
;
45 std::cout
<< __FILE__
<< ":" << __LINE__
<< std::endl
;
46 async_func_pt
<RetType
>* p
= new async_func_pt
<RetType
> (boost::packaged_task
<RetType
>(boost::forward
<F
>(f
)));
47 std::cout
<< __FILE__
<< ":" << __LINE__
<< std::endl
;
48 boost::unique_future
<RetType
> future_result
= p
->get_future();
49 std::cout
<< __FILE__
<< ":" << __LINE__
<< std::endl
;
51 std::cout
<< __FILE__
<< ":" << __LINE__
<< std::endl
;
52 return boost::move(future_result
);
55 template <typename F
, typename A1
>
56 boost::unique_future
<typename
boost::result_of
< F(A1
) >::type
>
57 async (F
&& f
, A1
&& a1
)
59 std::cout
<< __FILE__
<< ":" << __LINE__
<< std::endl
;
60 // This should be all it needs. But get a funny error deep inside Boost.
61 // problem overloading with && ?
62 return async (boost::bind(f
,a1
));
66 int calculate_the_answer_to_life_the_universe_and_everything()
72 size_t foo (const std::string
& s
)
82 // most fundimental form:
83 std::cout
<< __FILE__
<< ":" << __LINE__
<< std::endl
;
84 boost::unique_future
<int> fi
= async (&calculate_the_answer_to_life_the_universe_and_everything
);
89 // This one chokes at compile time
90 std::cout
<< __FILE__
<< ":" << __LINE__
<< std::endl
;
91 boost::unique_future
<size_t> fut_1
= async (&foo
, "Life");
93 BOOST_TEST (fut_1
.get()== 4);
99 return boost::report_errors();
112 "/Users/viboes/clang/llvmCore-3.0-rc1.install/bin/clang++"
113 -o "../../../bin.v2/libs/thread/test/test_ml.test/clang-darwin-3.0x/debug/threading-multi/test_ml"
114 "../../../bin.v2/libs/thread/test/test_ml.test/clang-darwin-3.0x/debug/threading-multi/test_ml.o"
115 "../../../bin.v2/libs/test/build/clang-darwin-3.0x/debug/link-static/threading-multi/libboost_unit_test_framework.a"
116 "../../../bin.v2/libs/thread/build/clang-darwin-3.0x/debug/threading-multi/libboost_thread.dylib"
117 "../../../bin.v2/libs/chrono/build/clang-darwin-3.0x/debug/threading-multi/libboost_chrono.dylib"
118 "../../../bin.v2/libs/system/build/clang-darwin-3.0x/debug/threading-multi/libboost_system.dylib" -g
124 * #include <boost/test/unit_test.hpp>
125 #include <boost/thread/future.hpp>
126 #include <boost/utility/result_of.hpp>
127 #include <functional>
130 virtual ~async_func() { }
131 virtual void run() =0;
134 template <typename Ret>
135 class async_func_pt : public async_func {
136 boost::packaged_task<Ret> f;
138 void run() override { f(); }
139 async_func_pt (boost::packaged_task<Ret>&& f) : f(std::move(f)) {}
141 boost::unique_future<Ret> get_future() { return f.get_future(); }
144 void async_core (async_func* p);
149 template <typename F>
150 boost::unique_future<typename boost::result_of< F() >::type>
153 typedef typename boost::result_of< F() >::type RetType;
154 async_func_pt<RetType>* p= new async_func_pt<RetType> (boost::packaged_task<RetType>(f));
155 boost::unique_future<RetType> future_result= p->get_future();
157 return std::move(future_result);
160 template <typename F, typename A1>
161 boost::unique_future<typename boost::result_of< F(A1) >::type>
162 async (F&& f, A1&& a1)
164 // This should be all it needs. But get a funny error deep inside Boost.
165 // problem overloading with && ?
166 return async (std::tr1::bind(f,a1));
169 BOOST_AUTO_TEST_SUITE(thread_pool_tests)
171 int calculate_the_answer_to_life_the_universe_and_everything()
177 size_t foo (const std::string& s)
184 BOOST_AUTO_TEST_CASE( async_test )
187 // most fundimental form:
188 boost::unique_future<int> fi= async (&calculate_the_answer_to_life_the_universe_and_everything);
190 BOOST_CHECK_EQUAL (i, 42);
193 // This one chokes at compile time
194 boost::unique_future<size_t> fut_1= async (&foo, "Life");
196 BOOST_CHECK_EQUAL (fut_1.get(), 4);
200 BOOST_AUTO_TEST_SUITE_END()