STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Public Types | Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | Friends | List of all members
Concurrency::task< void > Class Template Reference

The Parallel Patterns Library (PPL) task class. A task object represents work that can be executed asynchronously, and concurrently with other tasks and parallel work produced by parallel algorithms in the Concurrency Runtime. It produces a result of type _ResultType on successful completion. Tasks of type task<void> produce no result. A task can be waited upon and canceled independently of other tasks. It can also be composed with other tasks using continuations(then), and join(when_all) and choice(when_any) patterns. More...

#include <ppltasks.h>

Public Types

typedef void result_type
 The type of the result an object of this class produces. More...
 

Public Member Functions

 task ()
 Constructs a task object. More...
 
template<typename _Ty >
 __declspec (noinline) explicit task(_Ty _Param
 Constructs a task object. More...
 
return _M_unitTask _ThenImpl (typename details::_ContinuationTypeTraits< _Function, void >::_StdFuncT(_Func), _Options)
 
template<typename _Function >
 __declspec (noinline) auto then(const _Function &_Func
 Adds a continuation task to this task. More...
 
 details::_get_internal_task_options (_TaskOptions)._set_creation_callstack(_CAPTURE_CALLSTACK())
 
return _M_unitTask _ThenImpl (typename details::_ContinuationTypeTraits< _Function, void >::_StdFuncT(_Func), _Options)
 
task_status wait () const
 Waits for this task to reach a terminal state. It is possible for wait to execute the task inline, if all of the tasks dependencies are satisfied, and it has not already been picked up for execution by a background worker. More...
 
void get () const
 Returns the result this task produced. If the task is not in a terminal state, a call to get will wait for the task to finish. This method does not return a value when called on a task with a result_type of void. More...
 
bool is_done () const
 Determines if the task is completed. More...
 
scheduler_ptr scheduler () const
 Returns the scheduler for this task More...
 
bool is_apartment_aware () const
 Determines whether the task unwraps a Windows Runtime IAsyncInfo interface or is descended from such a task. More...
 
bool operator== (const task< void > &_Rhs) const
 Determines whether two task objects represent the same internal task. More...
 
bool operator!= (const task< void > &_Rhs) const
 Determines whether two task objects represent different internal tasks. More...
 
void _CreateImpl (details::_CancellationTokenState *_Ct, scheduler_ptr _Scheduler)
 Create an underlying task implementation. More...
 
const details::_Task_ptr< details::_Unit_type >::_Type & _GetImpl () const
 Return the underlying implementation for this task. More...
 
const details::_Task_ptr< details::_Unit_type >::_Type & _ThenGetImpl () const
 
void _SetImpl (const details::_Task_ptr< details::_Unit_type >::_Type &_Impl)
 Set the implementation of the task to be the supplied implementation. More...
 
void _SetImpl (details::_Task_ptr< details::_Unit_type >::_Type &&_Impl)
 Set the implementation of the task to be the supplied implementation using a move instead of a copy. More...
 
void _SetAsync (bool _Async=true)
 Sets a property determining whether the task is apartment aware. More...
 
void _SetTaskCreationCallstack (const details::_TaskCreationCallstack &_callstack)
 Sets a field in the task impl to the return callstack for calls to the task constructors and the then method. More...
 
template<typename _Function >
auto _Then (const _Function &_Func, details::_CancellationTokenState *_PTokenState, details::_TaskInliningMode_t _InliningMode=details::_ForceInline) const -> typename details::_ContinuationTypeTraits< _Function, void >::_TaskOfType
 An internal version of then that takes additional flags and executes the continuation inline. Used for runtime internal continuations only. More...
 

Public Attributes

const task_options_TaskOptions
 
auto _ContinuationContext = _TaskOptions.get_continuation_context()
 
auto _Options = details::_ThenImplOptions::_CreateOptions(_TaskOptions, _ContinuationContext, _ThenGetImpl()->_GetScheduler())
 
cancellation_token _CancellationToken
 

Private Member Functions

void _TaskInitNoFunctor (task_completion_event< void > &_Event)
 Initializes a task using a task completion event. More...
 
template<typename _Function >
void _TaskInitMaybeFunctor (_Function &_Func, std::true_type)
 Initializes a task using a callable object. More...
 
template<typename _Ty >
void _TaskInitMaybeFunctor (_Ty &_Param, std::false_type)
 Initializes a task using a non-callable object. More...
 

Private Attributes

task< details::_Unit_type_M_unitTask
 

Friends

template<typename _Ty >
class task
 
template<typename _Ty >
class task_completion_event
 

Detailed Description

template<>
class Concurrency::task< void >

The Parallel Patterns Library (PPL) task class. A task object represents work that can be executed asynchronously, and concurrently with other tasks and parallel work produced by parallel algorithms in the Concurrency Runtime. It produces a result of type _ResultType on successful completion. Tasks of type task<void> produce no result. A task can be waited upon and canceled independently of other tasks. It can also be composed with other tasks using continuations(then), and join(when_all) and choice(when_any) patterns.

For more information, see Task Parallelism (Concurrency Runtime).

Member Typedef Documentation

The type of the result an object of this class produces.

Constructor & Destructor Documentation

Concurrency::task< void >::task ( )
inline

Constructs a task object.

The default constructor for a task is only present in order to allow tasks to be used within containers. A default constructed task cannot be used until you assign a valid task to it. Methods such as get, wait or then will throw an invalid_argument exception when called on a default constructed task.

A task that is created from a task_completion_event will complete (and have its continuations scheduled) when the task completion event is set.

The version of the constructor that takes a cancellation token creates a task that can be canceled using the cancellation_token_source the token was obtained from. Tasks created without a cancellation token are not cancelable.

Tasks created from a Windows::Foundation::IAsyncInfo interface or a lambda that returns an IAsyncInfo interface reach their terminal state when the enclosed Windows Runtime asynchronous operation or action completes. Similarly, tasks created from a lambda that returns a task<result_type> reach their terminal state when the inner task reaches its terminal state, and not when the lambda returns.

task behaves like a smart pointer and is safe to pass around by value. It can be accessed by multiple threads without the need for locks.

The constructor overloads that take a Windows::Foundation::IAsyncInfo interface or a lambda returning such an interface, are only available to Windows Store apps.

For more information, see Task Parallelism (Concurrency Runtime).

3929  : _M_unitTask()
3930  {
3931  // The default constructor should create a task with a nullptr impl. This is a signal that the
3932  // task is not usable and should throw if any wait(), get() or then() APIs are used.
3933  }
task< details::_Unit_type > _M_unitTask
Definition: ppltasks.h:4380

Member Function Documentation

template<typename _Ty >
Concurrency::task< void >::__declspec ( noinline  )

Constructs a task object.

Template Parameters
_TyThe type of the parameter from which the task is to be constructed.
Parameters
_ParamThe parameter from which the task is to be constructed. This could be a lambda, a function object, a task_completion_event<result_type> object, or a Windows::Foundation::IAsyncInfo if you are using tasks in your Windows Store app. The lambda or function object should be a type equivalent to std::function<X(void)>, where X can be a variable of type result_type, task<result_type>, or a Windows::Foundation::IAsyncInfo in Windows Store apps.

The default constructor for a task is only present in order to allow tasks to be used within containers. A default constructed task cannot be used until you assign a valid task to it. Methods such as get, wait or then will throw an invalid_argument exception when called on a default constructed task.

A task that is created from a task_completion_event will complete (and have its continuations scheduled) when the task completion event is set.

The version of the constructor that takes a cancellation token creates a task that can be canceled using the cancellation_token_source the token was obtained from. Tasks created without a cancellation token are not cancelable.

Tasks created from a Windows::Foundation::IAsyncInfo interface or a lambda that returns an IAsyncInfo interface reach their terminal state when the enclosed Windows Runtime asynchronous operation or action completes. Similarly, tasks created from a lambda that returns a task<result_type> reach their terminal state when the inner task reaches its terminal state, and not when the lambda returns.

task behaves like a smart pointer and is safe to pass around by value. It can be accessed by multiple threads without the need for locks.

The constructor overloads that take a Windows::Foundation::IAsyncInfo interface or a lambda returning such an interface, are only available to Windows Store apps.

For more information, see Task Parallelism (Concurrency Runtime).

template<typename _Function >
Concurrency::task< void >::__declspec ( noinline  ) const

Adds a continuation task to this task.

Template Parameters
_FunctionThe type of the function object that will be invoked by this task.
Parameters
_FuncThe continuation function to execute when this task completes. This continuation function must take as input a variable of either result_type or task<result_type>, where result_type is the type of the result this task produces.
_CancellationTokenThe cancellation token to associate with the continuation task. A continuation task that is created without a cancellation token will inherit the token of its antecedent task.
_ContinuationContextA variable that specifies where the continuation should execute. This variable is only useful when used in a Windows Store style app. For more information, see task_continuation_context
Returns
The newly created continuation task. The result type of the returned task is determined by what _Func returns.

The overloads of then that take a lambda or functor that returns a Windows::Foundation::IAsyncInfo interface, are only available to Windows Store apps.

For more information on how to use task continuations to compose asynchronous work, see Task Parallelism (Concurrency Runtime).

void Concurrency::task< void >::_CreateImpl ( details::_CancellationTokenState _Ct,
scheduler_ptr  _Scheduler 
)
inline

Create an underlying task implementation.

4253  {
4254  _M_unitTask._CreateImpl(_Ct, _Scheduler);
4255  }
task< details::_Unit_type > _M_unitTask
Definition: ppltasks.h:4380
_CreateImpl(_TaskOptions.get_cancellation_token()._GetImplValue(), _TaskOptions.get_scheduler())
const details::_Task_ptr<details::_Unit_type>::_Type& Concurrency::task< void >::_GetImpl ( ) const
inline

Return the underlying implementation for this task.

4261  {
4262  return _M_unitTask._M_Impl;
4263  }
task< details::_Unit_type > _M_unitTask
Definition: ppltasks.h:4380
details::_Task_ptr< _ReturnType >::_Type _M_Impl
Definition: ppltasks.h:3883
void Concurrency::task< void >::_SetAsync ( bool  _Async = true)
inline

Sets a property determining whether the task is apartment aware.

4295  {
4296  _M_unitTask._SetAsync(_Async);
4297  }
task< details::_Unit_type > _M_unitTask
Definition: ppltasks.h:4380
void _SetAsync(bool _Async=true)
Sets a property determining whether the task is apartment aware.
Definition: ppltasks.h:3406
void Concurrency::task< void >::_SetImpl ( const details::_Task_ptr< details::_Unit_type >::_Type &  _Impl)
inline

Set the implementation of the task to be the supplied implementation.

4279  {
4280  _M_unitTask._SetImpl(_Impl);
4281  }
task< details::_Unit_type > _M_unitTask
Definition: ppltasks.h:4380
void _SetImpl(const typename details::_Task_ptr< _ReturnType >::_Type &_Impl)
Set the implementation of the task to be the supplied implementation.
Definition: ppltasks.h:3388
void Concurrency::task< void >::_SetImpl ( details::_Task_ptr< details::_Unit_type >::_Type &&  _Impl)
inline

Set the implementation of the task to be the supplied implementation using a move instead of a copy.

4287  {
4288  _M_unitTask._SetImpl(std::move(_Impl));
4289  }
task< details::_Unit_type > _M_unitTask
Definition: ppltasks.h:4380
void _SetImpl(const typename details::_Task_ptr< _ReturnType >::_Type &_Impl)
Set the implementation of the task to be the supplied implementation.
Definition: ppltasks.h:3388
constexpr remove_reference< _Ty >::type && move(_Ty &&_Arg) _NOEXCEPT
Definition: type_traits:1290
void Concurrency::task< void >::_SetTaskCreationCallstack ( const details::_TaskCreationCallstack _callstack)
inline

Sets a field in the task impl to the return callstack for calls to the task constructors and the then method.

4303  {
4305  }
task< details::_Unit_type > _M_unitTask
Definition: ppltasks.h:4380
_SetTaskCreationCallstack(details::_get_internal_task_options(_TaskOptions)._M_hasPresetCreationCallstack?details::_get_internal_task_options(_TaskOptions)._M_presetCreationCallstack:_CAPTURE_CALLSTACK())
template<typename _Function >
void Concurrency::task< void >::_TaskInitMaybeFunctor ( _Function &  _Func,
std::true_type   
)
inlineprivate

Initializes a task using a callable object.

4366  {
4367  _M_unitTask._TaskInitWithFunctor<void, _Function>(_Func);
4368  }
task< details::_Unit_type > _M_unitTask
Definition: ppltasks.h:4380
void _TaskInitWithFunctor(const _Function &_Func)
Initializes a task using a lambda, function pointer or function object.
Definition: ppltasks.h:3775
_CRT_BEGIN_C_HEADER typedef void(__CRTDECL *unexpected_handler)()
template<typename _Ty >
void Concurrency::task< void >::_TaskInitMaybeFunctor ( _Ty &  _Param,
std::false_type   
)
inlineprivate

Initializes a task using a non-callable object.

4375  {
4376  _TaskInitNoFunctor(_Param);
4377  }
void _TaskInitNoFunctor(task_completion_event< void > &_Event)
Initializes a task using a task completion event.
Definition: ppltasks.h:4337
void Concurrency::task< void >::_TaskInitNoFunctor ( task_completion_event< void > &  _Event)
inlineprivate

Initializes a task using a task completion event.

4338  {
4339  _M_unitTask._TaskInitNoFunctor(_Event._M_unitEvent);
4340  }
task< details::_Unit_type > _M_unitTask
Definition: ppltasks.h:4380
void _TaskInitNoFunctor(task_completion_event< _ReturnType > &_Event)
Initializes a task using a task completion event.
Definition: ppltasks.h:3788
template<typename _Function >
auto Concurrency::task< void >::_Then ( const _Function &  _Func,
details::_CancellationTokenState _PTokenState,
details::_TaskInliningMode_t  _InliningMode = details::_ForceInline 
) const -> typename details::_ContinuationTypeTraits<_Function, void>::_TaskOfType
inline

An internal version of then that takes additional flags and executes the continuation inline. Used for runtime internal continuations only.

4313  {
4314 
4315  // inherit from antecedent
4316  auto _Scheduler = _ThenGetImpl()->_GetScheduler();
4317 
4318  // Note: _ThenImpl's implementation makes a copy of the continuation_context when it schedules the continuation. '
4319  // The Continuation Context used to create the _ThenImplOptions must still exist when _ThenImpl executes.
4320  auto _Default_Context = task_continuation_context::use_default();
4321  details::_ThenImplOptions _Options{ _PTokenState, &_Default_Context, _Scheduler, _CAPTURE_CALLSTACK(), _InliningMode };
4322 
4323 #ifndef _PPLTASKS_NO_STDFUNC
4325 #else
4326  return _M_unitTask._ThenImpl<void>(_Func, _Options);
4327 #endif
4328  }
task< details::_Unit_type > _M_unitTask
Definition: ppltasks.h:4380
auto _Options
Definition: ppltasks.h:4106
const details::_Task_ptr< details::_Unit_type >::_Type & _ThenGetImpl() const
Definition: ppltasks.h:4265
return _ThenImpl(typename details::_ContinuationTypeTraits< _Function, _ReturnType >::_StdFuncT(_Func), _Options)
#define _CAPTURE_CALLSTACK()
Helper macro to determine how many stack frames need to be saved. When any number less or equal to 1 ...
Definition: ppltasks.h:117
static task_continuation_context use_default()
Creates the default task continuation context.
Definition: ppltasks.h:936
std::function< _RetTypeT __cdecl(_ArgTypeT)> _StdFuncT
Definition: ppltasks.h:406
const details::_Task_ptr<details::_Unit_type>::_Type& Concurrency::task< void >::_ThenGetImpl ( ) const
inline
4266  {
4267  if (!_M_unitTask._M_Impl)
4268  {
4269  details::_DefaultTaskHelper::_NoCallOnDefaultTask_ErrorImpl();
4270  }
4271 
4272  return _M_unitTask._M_Impl;
4273  }
task< details::_Unit_type > _M_unitTask
Definition: ppltasks.h:4380
details::_Task_ptr< _ReturnType >::_Type _M_Impl
Definition: ppltasks.h:3883
return _M_unitTask Concurrency::task< void >::_ThenImpl ( typename details::_ContinuationTypeTraits< _Function, void >::  _StdFuncT_Func,
_Options   
)
return _M_unitTask Concurrency::task< void >::_ThenImpl ( typename details::_ContinuationTypeTraits< _Function, void >::  _StdFuncT_Func,
_Options   
)
void Concurrency::task< void >::get ( ) const
inline

Returns the result this task produced. If the task is not in a terminal state, a call to get will wait for the task to finish. This method does not return a value when called on a task with a result_type of void.

If the task is canceled, a call to get will throw a task_canceled exception. If the task encountered an different exception or an exception was propagated to it from an antecedent task, a call to get will throw that exception.

4184  {
4185  _M_unitTask.get();
4186  }
task< details::_Unit_type > _M_unitTask
Definition: ppltasks.h:4380
_ReturnType get() const
Returns the result this task produced. If the task is not in a terminal state, a call to get will wai...
Definition: ppltasks.h:3264
bool Concurrency::task< void >::is_apartment_aware ( ) const
inline

Determines whether the task unwraps a Windows Runtime IAsyncInfo interface or is descended from such a task.

Returns
true if the task unwraps an IAsyncInfo interface or is descended from such a task, false otherwise.
4221  {
4223  }
task< details::_Unit_type > _M_unitTask
Definition: ppltasks.h:4380
bool is_apartment_aware() const
Determines whether the task unwraps a Windows Runtime IAsyncInfo interface or is descended from such ...
Definition: ppltasks.h:3321
bool Concurrency::task< void >::is_done ( ) const
inline

Determines if the task is completed.

Returns
True if the task has completed, false otherwise.

The function returns true if the task is completed or canceled (with or without user exception).

4198  {
4199  return _M_unitTask.is_done();
4200  }
task< details::_Unit_type > _M_unitTask
Definition: ppltasks.h:4380
bool is_done() const
Determines if the task is completed.
Definition: ppltasks.h:3288
bool Concurrency::task< void >::operator!= ( const task< void > &  _Rhs) const
inline

Determines whether two task objects represent different internal tasks.

Returns
true if the objects refer to different underlying tasks, and false otherwise.
4245  {
4246  return !operator==(_Rhs);
4247  }
bool operator==(const task< void > &_Rhs) const
Determines whether two task objects represent the same internal task.
Definition: ppltasks.h:4232
bool Concurrency::task< void >::operator== ( const task< void > &  _Rhs) const
inline

Determines whether two task objects represent the same internal task.

Returns
true if the objects refer to the same underlying task, and false otherwise.
4233  {
4234  return (_M_unitTask == _Rhs._M_unitTask);
4235  }
task< details::_Unit_type > _M_unitTask
Definition: ppltasks.h:4380
scheduler_ptr Concurrency::task< void >::scheduler ( ) const
inline

Returns the scheduler for this task

Returns
A pointer to the scheduler
4209  {
4210  return _M_unitTask.scheduler();
4211  }
task< details::_Unit_type > _M_unitTask
Definition: ppltasks.h:4380
scheduler_ptr scheduler() const
Returns the scheduler for this task
Definition: ppltasks.h:3304
task_status Concurrency::task< void >::wait ( ) const
inline

Waits for this task to reach a terminal state. It is possible for wait to execute the task inline, if all of the tasks dependencies are satisfied, and it has not already been picked up for execution by a background worker.

Returns
A task_status value which could be either completed or canceled. If the task encountered an exception during execution, or an exception was propagated to it from an antecedent task, wait will throw that exception.
4170  {
4171  return _M_unitTask.wait();
4172  }
task< details::_Unit_type > _M_unitTask
Definition: ppltasks.h:4380
task_status wait() const
Waits for this task to reach a terminal state. It is possible for wait to execute the task inline...
Definition: ppltasks.h:3242

Friends And Related Function Documentation

template<typename _Ty >
friend class task
friend
template<typename _Ty >
friend class task_completion_event
friend

Member Data Documentation

cancellation_token Concurrency::task< void >::_CancellationToken
auto Concurrency::task< void >::_ContinuationContext = _TaskOptions.get_continuation_context()
task<details::_Unit_type> Concurrency::task< void >::_M_unitTask
private
const task_options& Concurrency::task< void >::_TaskOptions

The documentation for this class was generated from the following file: