...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
A packaged_task<>
wraps a callable target that
returns a value so that the return value can be computed asynchronously.
Conventional usage of packaged_task<>
is like this:
packaged_task<>
with template arguments matching
the signature of the callable. Pass the callable to the constructor.
packaged_task::get_future()
and capture
the returned future<>
instance.
fiber
to run the new packaged_task<>
, passing any arguments required
by the original callable.
fiber::detach()
on the newly-launched fiber
.
future<>
.
This is, in fact, pretty much what fibers::async()
encapsulates.
#include <boost/fiber/future/packaged_task.hpp> namespace boost { namespace fibers { template< class R, typename ... Args > class packaged_task< R( Args ... ) > { public: packaged_task() noexcept; template< typename Fn > explicit packaged_task( Fn &&); template< typename Fn, typenameAllocator
> packaged_task(std::allocator_arg_t
, Allocator const&, Fn &&); packaged_task( packaged_task &&) noexcept; packaged_task & operator=( packaged_task &&) noexcept; packaged_task( packaged_task const&) = delete; packaged_task & operator=( packaged_task const&) = delete; ~packaged_task(); void swap( packaged_task &) noexcept; bool valid() const noexcept; future< R > get_future(); void operator()( Args ...); void reset(); }; template< typename Signature > void swap( packaged_task< Signature > &, packaged_task< Signature > &) noexcept; }}
packaged_task()
packaged_task() noexcept;
Constructs an object of class packaged_task
with no shared state.
Nothing.
packaged_task()
template< typename Fn > explicit packaged_task( Fn && fn); template< typename Fn, typenameAllocator
> packaged_task(std::allocator_arg_t
, Allocator const& alloc, Fn && fn);
Constructs an object of class packaged_task
with a shared state and copies
or moves the callable target fn
to internal storage.
Exceptions caused by memory allocation.
The signature of Fn
should have a return type convertible to R
.
packaged_task( packaged_task && other) noexcept;
Creates a packaged_task by moving the shared
state from other
.
other
contains no
valid shared state.
Nothing.
~packaged_task();
Destroys *this
and abandons the shared state
if shared state is ready; otherwise stores future_error
with error condition future_errc::broken_promise
as if by promise::set_exception()
: the shared
state is set ready.
operator=
()
packaged_task & operator=( packaged_task && other) noexcept;
Transfers the ownership of shared state
to *this
.
other
contains no
valid shared state.
Nothing.
swap
()
void swap( packaged_task & other) noexcept;
Swaps the shared state between
other and *this
.
Nothing.
valid
()
bool valid() const noexcept;
Returns true
if *this
contains a shared state.
Nothing.
get_future
()
future< R > get_future();
A future<>
with the same shared
state.
future_error
with
future_errc::future_already_retrieved
or future_errc::no_state
.
operator()
()
void operator()( Args && ... args);
Invokes the stored callable target. Any exception thrown by the callable
target fn
is stored
in the shared state as if by
promise::set_exception()
. Otherwise, the value
returned by fn
is
stored in the shared state as if by promise::set_value()
.
future_error
with
future_errc::no_state
.
reset
()
void reset();
Resets the shared state and abandons the result of previous executions. A new shared state is constructed.
future_error
with
future_errc::no_state
.
swap()
template< typename Signature > void swap( packaged_task< Signature > & l, packaged_task< Signature > & r) noexcept;
Same as l.swap(
r)
.