description | title | ms.date | f1_keywords | ms.assetid | helpviewer_keywords | ms.custom | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Learn more about: packaged_task Class |
packaged_task Class |
06/17/2022 |
|
0a72cbe3-f22a-4bfe-8e50-dcb268c98780 |
|
devdivchpfy22 |
Describes an asynchronous provider that is a call wrapper whose call signature is Ty(ArgTypes...)
. Its associated asynchronous state holds a copy of its callable object in addition to the potential result.
template <class>
class packaged_task;
Name | Description |
---|---|
packaged_task | Constructs a packaged_task object. |
packaged_task::~packaged_task Destructor | Destroys a packaged_task object. |
Name | Description |
---|---|
get_future | Returns a future object that has the same associated asynchronous state. |
make_ready_at_thread_exit | Calls the callable object that's stored in the associated asynchronous state and atomically stores the returned value. |
reset | Replaces the associated asynchronous state. |
swap | Exchanges the associated asynchronous state with a specified object. |
valid | Specifies whether the object has an associated asynchronous state. |
Name | Description |
---|---|
packaged_task::operator= | Transfers an associated asynchronous state from a specified object. |
packaged_task::operator() | Calls the callable object that's stored in the associated asynchronous state, atomically stores the returned value, and sets the state to ready. |
packaged_task::operator bool | Specifies whether the object has an associated asynchronous state. |
Header: <future>
Namespace: std
Returns an object of type future<Ty>
that has the same associated asynchronous state.
future<Ty> get_future();
If the packaged_task
object doesn't have an associated asynchronous state, this method throws a future_error that has an error code of no_state
.
If this method has already been called for a packaged_task
object that has the same associated asynchronous state, the method throws a future_error
that has an error code of future_already_retrieved
.
Calls the callable object that's stored in the associated asynchronous state and atomically stores the returned value.
void make_ready_at_thread_exit(ArgTypes... args);
If the packaged_task
object doesn't have an associated asynchronous state, this method throws a future_error that has an error code of no_state
.
If this method or make_ready_at_thread_exit has already been called for a packaged_task
object that has the same associated asynchronous state, the method throws a future_error
that has an error code of promise_already_satisfied
.
Otherwise, this operator calls INVOKE(fn, args..., Ty)
, where fn is the callable object that's stored in the associated asynchronous state. Any returned value is stored atomically as the returned result of the associated asynchronous state.
In contrast to packaged_task::operator(), the associated asynchronous state isn't set to ready
until after all thread-local objects in the calling thread have been destroyed. Typically, threads that are blocked on the associated asynchronous state aren't unblocked until the calling thread exits.
Transfers the associated asynchronous state from a specified object.
packaged_task& operator=(packaged_task&& Right);
Right
A packaged_task
object.
*this
After the operation, Right no longer has an associated asynchronous state.
Calls the callable object that's stored in the associated asynchronous state, atomically stores the returned value, and sets the state to ready.
void operator()(ArgTypes... args);
If the packaged_task
object doesn't have an associated asynchronous state, this method throws a future_error that has an error code of no_state
.
If this method or make_ready_at_thread_exit has already been called for a packaged_task
object that has the same associated asynchronous state, the method throws a future_error
that has an error code of promise_already_satisfied
.
Otherwise, this operator calls INVOKE(fn, args..., Ty)
, where fn is the callable object that's stored in the associated asynchronous state. Any returned value is stored atomically as the returned result of the associated asynchronous state, and the state is set to ready. As a result, any threads that are blocked on the associated asynchronous state become unblocked.
Specifies whether the object has an associated asynchronous state
.
operator bool() const noexcept;
true
if the object has an associated asynchronous state; otherwise, false
.
Constructs a packaged_task
object.
packaged_task() noexcept;
packaged_task(packaged_task&& Right) noexcept;
template <class Fn>
explicit packaged_task(Fn&& fn);
template <class Fn, class Alloc>
explicit packaged_task(
allocator_arg_t, const Alloc& alloc, Fn&& fn);
Right
A packaged_task
object.
alloc
A memory allocator. For more information, see <allocators>.
fn
A function object.
The first constructor constructs a packaged_task
object that has no associated asynchronous state.
The second constructor constructs a packaged_task
object and transfers the associated asynchronous state from Right. After the operation, Right no longer has an associated asynchronous state.
The third constructor constructs a packaged_task
object that has a copy of fn stored in its associated asynchronous state.
The fourth constructor constructs a packaged_task
object that has a copy of fn stored in its associated asynchronous state, and uses alloc
for memory allocation.
Destroys a packaged_task
object.
~packaged_task();
If the associated asynchronous state isn't ready, the destructor stores a future_error exception that has an error code of broken_promise
as the result in the associated asynchronous state, and any threads that are blocked on the associated asynchronous state become unblocked.
Uses a new associated asynchronous state to replace the existing associated asynchronous state.
void reset();
In effect, this method executes *this = packaged_task(move(fn))
, where fn is the function object that's stored in the associated asynchronous state for this object. Therefore, the state of the object is cleared, and get_future, operator(), and make_ready_at_thread_exit can be called as if on a newly constructed object.
Exchanges the associated asynchronous state with a specified object.
void swap(packaged_task& Right) noexcept;
Right
A packaged_task
object.
Specifies whether the object has an associated asynchronous state
.
bool valid() const;
true
if the object has an associated asynchronous state; otherwise, false
.