2 // Copyright Oliver Kowalke 2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_FIBERS_ASYNC_HPP
8 #define BOOST_FIBERS_ASYNC_HPP
12 #include <type_traits>
15 #include <boost/config.hpp>
17 #include <boost/fiber/future/future.hpp>
18 #include <boost/fiber/future/packaged_task.hpp>
19 #include <boost/fiber/policy.hpp>
24 template< typename Fn, typename ... Args >
26 typename std::result_of<
27 typename std::enable_if<
28 ! detail::is_launch_policy< typename std::decay< Fn >::type >::value,
29 typename std::decay< Fn >::type
30 >::type( typename std::decay< Args >::type ... )
33 async( Fn && fn, Args ... args) {
34 typedef typename std::result_of<
35 typename std::decay< Fn >::type( typename std::decay< Args >::type ... )
38 packaged_task< result_type( typename std::decay< Args >::type ... ) > pt{
39 std::forward< Fn >( fn) };
40 future< result_type > f{ pt.get_future() };
41 fiber{ std::move( pt), std::forward< Args >( args) ... }.detach();
45 template< typename Policy, typename Fn, typename ... Args >
47 typename std::result_of<
48 typename std::enable_if<
49 detail::is_launch_policy< Policy >::value,
50 typename std::decay< Fn >::type
51 >::type( typename std::decay< Args >::type ...)
54 async( Policy policy, Fn && fn, Args ... args) {
55 typedef typename std::result_of<
56 typename std::decay< Fn >::type( typename std::decay< Args >::type ... )
59 packaged_task< result_type( typename std::decay< Args >::type ... ) > pt{
60 std::forward< Fn >( fn) };
61 future< result_type > f{ pt.get_future() };
62 fiber{ policy, std::move( pt), std::forward< Args >( args) ... }.detach();
66 template< typename Policy, typename StackAllocator, typename Fn, typename ... Args >
68 typename std::result_of<
69 typename std::enable_if<
70 detail::is_launch_policy< Policy >::value,
71 typename std::decay< Fn >::type
72 >::type( typename std::decay< Args >::type ... )
75 async( Policy policy, std::allocator_arg_t, StackAllocator salloc, Fn && fn, Args ... args) {
76 typedef typename std::result_of<
77 typename std::decay< Fn >::type( typename std::decay< Args >::type ... )
80 packaged_task< result_type( typename std::decay< Args >::type ... ) > pt{
81 std::forward< Fn >( fn) };
82 future< result_type > f{ pt.get_future() };
83 fiber{ policy, std::allocator_arg, salloc,
84 std::move( pt), std::forward< Args >( args) ... }.detach();
88 template< typename Policy, typename StackAllocator, typename Allocator, typename Fn, typename ... Args >
90 typename std::result_of<
91 typename std::enable_if<
92 detail::is_launch_policy< Policy >::value,
93 typename std::decay< Fn >::type
94 >::type( typename std::decay< Args >::type ... )
97 async( Policy policy, std::allocator_arg_t, StackAllocator salloc, Allocator alloc, Fn && fn, Args ... args) {
98 typedef typename std::result_of<
99 typename std::decay< Fn >::type( typename std::decay< Args >::type ... )
102 packaged_task< result_type( typename std::decay< Args >::type ... ) > pt{
103 std::allocator_arg, alloc, std::forward< Fn >( fn) };
104 future< result_type > f{ pt.get_future() };
105 fiber{ policy, std::allocator_arg, salloc,
106 std::move( pt), std::forward< Args >( args) ... }.detach();
112 #endif // BOOST_FIBERS_ASYNC_HPP