STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Public Member Functions | Private Member Functions | Private Attributes | List of all members
Concurrency::task_group Class Reference

The task_group class represents a collection of parallel work which can be waited on or canceled. More...

#include <ppl.h>

Public Member Functions

 task_group ()
 Constructs a new task_group object. More...
 
 task_group (cancellation_token _CancellationToken)
 Constructs a new task_group object. More...
 
 ~task_group ()
 Destroys a task_group object. You are expected to call the either the wait or run_and_wait method on the object prior to the destructor executing, unless the destructor is executing as the result of stack unwinding due to an exception. More...
 
template<typename _Function >
void run (const _Function &_Func)
 Schedules a task on the task_group object. If a task_handle object is passed as a parameter to run, the caller is responsible for managing the lifetime of the task_handle object. The version of the method that takes a reference to a function object as a parameter involves heap allocation inside the runtime which may be perform less well than using the version that takes a reference to a task_handle object. The version which takes the parameter _Placement causes the task to be biased towards executing at the location specified by that parameter. More...
 
template<typename _Function >
void run (const _Function &_Func, location &_Placement)
 Schedules a task on the task_group object. If a task_handle object is passed as a parameter to run, the caller is responsible for managing the lifetime of the task_handle object. The version of the method that takes a reference to a function object as a parameter involves heap allocation inside the runtime which may be perform less well than using the version that takes a reference to a task_handle object. The version which takes the parameter _Placement causes the task to be biased towards executing at the location specified by that parameter. More...
 
template<typename _Function >
void run (task_handle< _Function > &_Task_handle)
 Schedules a task on the task_group object. If a task_handle object is passed as a parameter to run, the caller is responsible for managing the lifetime of the task_handle object. The version of the method that takes a reference to a function object as a parameter involves heap allocation inside the runtime which may be perform less well than using the version that takes a reference to a task_handle object. The version which takes the parameter _Placement causes the task to be biased towards executing at the location specified by that parameter. More...
 
template<typename _Function >
void run (task_handle< _Function > &_Task_handle, location &_Placement)
 Schedules a task on the task_group object. If a task_handle object is passed as a parameter to run, the caller is responsible for managing the lifetime of the task_handle object. The version of the method that takes a reference to a function object as a parameter involves heap allocation inside the runtime which may be perform less well than using the version that takes a reference to a task_handle object. The version which takes the parameter _Placement causes the task to be biased towards executing at the location specified by that parameter. More...
 
task_group_status wait ()
 Waits until all work on the task_group object has either completed or been canceled. More...
 
template<class _Function >
task_group_status run_and_wait (task_handle< _Function > &_Task_handle)
 Schedules a task to be run inline on the calling context with the assistance of the task_group object for full cancellation support. The function then waits until all work on the task_group object has either completed or been canceled. If a task_handle object is passed as a parameter to run_and_wait, the caller is responsible for managing the lifetime of the task_handle object. More...
 
template<class _Function >
task_group_status run_and_wait (const _Function &_Func)
 Schedules a task to be run inline on the calling context with the assistance of the task_group object for full cancellation support. The function then waits until all work on the task_group object has either completed or been canceled. If a task_handle object is passed as a parameter to run_and_wait, the caller is responsible for managing the lifetime of the task_handle object. More...
 
void cancel ()
 Makes a best effort attempt to cancel the sub-tree of work rooted at this task group. Every task scheduled on the task group will get canceled transitively if possible. More...
 
bool is_canceling ()
 Informs the caller whether or not the task group is currently in the midst of a cancellation. This does not necessarily indicate that the cancel method was called on the task_group object (although such certainly qualifies this method to return true). It may be the case that the task_group object is executing inline and a task group further up in the work tree was canceled. In cases such as these where the runtime can determine ahead of time that cancellation will flow through this task_group object, true will be returned as well. More...
 

Private Member Functions

template<class _Function >
void run (task_handle< _Function > &&_Task_handle)
 

Private Attributes

::Concurrency::details::_TaskCollection _M_task_collection
 

Detailed Description

The task_group class represents a collection of parallel work which can be waited on or canceled.

Unlike the heavily restricted structured_task_group class, the task_group class is much more general construct. It does not have any of the restrictions described by structured_task_group. task_group objects may safely be used across threads and utilized in free-form ways. The disadvantage of the task_group construct is that it may not perform as well as the structured_task_group construct for tasks which perform small amounts of work.

For more information, see Task Parallelism.

See also
structured_task_group Class, task_handle Class

Constructor & Destructor Documentation

Concurrency::task_group::task_group ( )
inline

Constructs a new task_group object.

The constructor that takes a cancellation token creates a task_group that will be canceled when the source associated with the token is canceled. Providing an explicit cancellation token also isolates this task group from participating in an implicit cancellation from a parent group with a different token or no token.

See also
Task Parallelism
504  {
505  }
Concurrency::task_group::task_group ( cancellation_token  _CancellationToken)
inline

Constructs a new task_group object.

Parameters
_CancellationTokenA cancellation token to associate with this task group. The task group will be canceled when the token is canceled.

The constructor that takes a cancellation token creates a task_group that will be canceled when the source associated with the token is canceled. Providing an explicit cancellation token also isolates this task group from participating in an implicit cancellation from a parent group with a different token or no token.

See also
Task Parallelism
519  :
520  _M_task_collection(_CancellationToken._GetImpl() != NULL ? _CancellationToken._GetImpl() : Concurrency::details::_CancellationTokenState::_None())
521  {
522  }
::Concurrency::details::_TaskCollection _M_task_collection
Definition: ppl.h:841
#define NULL
Definition: crtdbg.h:30
static _CancellationTokenState * _None()
Definition: pplcancellation_token.h:328
Concurrency::task_group::~task_group ( )
inline

Destroys a task_group object. You are expected to call the either the wait or run_and_wait method on the object prior to the destructor executing, unless the destructor is executing as the result of stack unwinding due to an exception.

If the destructor runs as the result of normal execution (for example, not stack unwinding due to an exception) and neither the wait nor run_and_wait methods have been called, the destructor may throw a missing_wait exception.

See also
task_group::wait Method, task_group::run_and_wait Method
536  {
537  }

Member Function Documentation

void Concurrency::task_group::cancel ( )
inline

Makes a best effort attempt to cancel the sub-tree of work rooted at this task group. Every task scheduled on the task group will get canceled transitively if possible.

For more information, see Cancellation in the PPL.

812  {
814  }
::Concurrency::details::_TaskCollection _M_task_collection
Definition: ppl.h:841
_CRTIMP void _Cancel()
Cancels work on the task collection.
bool Concurrency::task_group::is_canceling ( )
inline

Informs the caller whether or not the task group is currently in the midst of a cancellation. This does not necessarily indicate that the cancel method was called on the task_group object (although such certainly qualifies this method to return true). It may be the case that the task_group object is executing inline and a task group further up in the work tree was canceled. In cases such as these where the runtime can determine ahead of time that cancellation will flow through this task_group object, true will be returned as well.

Returns
An indication of whether the task_group object is in the midst of a cancellation (or is guaranteed to be shortly).

For more information, see Cancellation in the PPL.

831  {
833  }
::Concurrency::details::_TaskCollection _M_task_collection
Definition: ppl.h:841
_CRTIMP bool _IsCanceling()
Informs the caller whether or not the task collection is currently in the midst of a cancellation...
template<typename _Function >
void Concurrency::task_group::run ( const _Function &  _Func)
inline

Schedules a task on the task_group object. If a task_handle object is passed as a parameter to run, the caller is responsible for managing the lifetime of the task_handle object. The version of the method that takes a reference to a function object as a parameter involves heap allocation inside the runtime which may be perform less well than using the version that takes a reference to a task_handle object. The version which takes the parameter _Placement causes the task to be biased towards executing at the location specified by that parameter.

Template Parameters
_FunctionThe type of the function object that will be invoked to execute the body of the task handle.
Parameters
_FuncA function which will be called to invoke the body of the task. This may be a lambda expression or other object which supports a version of the function call operator with the signature void operator()().

The runtime schedules the provided work function to run at a later time, which can be after the calling function returns. This method uses a task_handle object to hold a copy of the provided work function. Therefore, any state changes that occur in a function object that you pass to this method will not appear in your copy of that function object. In addition, make sure that the lifetime of any objects that you pass by pointer or by reference to the work function remain valid until the work function returns.

If the task_group destructs as the result of stack unwinding from an exception, you do not need to guarantee that a call has been made to either the wait or run_and_wait method. In this case, the destructor will appropriately cancel and wait for the task represented by the _Task_handle parameter to complete.

The method throws an invalid_multiple_scheduling exception if the task handle given by the _Task_handle parameter has already been scheduled onto a task group object via the run method and there has been no intervening call to either the wait or run_and_wait method on that task group.

See also
task_group::wait Method, task_group::run_and_wait Method, Task Parallelism, location class
573  {
574  _M_task_collection._Schedule(::Concurrency::details::_UnrealizedChore::_InternalAlloc<task_handle<_Function>, _Function>(_Func));
575  }
::Concurrency::details::_TaskCollection _M_task_collection
Definition: ppl.h:841
_CRTIMP void _Schedule(_UnrealizedChore *_PChore, location *_PLocation)
Schedules a chore that can potentially run in parallel. The chore is pushed onto the associated works...
static _ChoreType * _InternalAlloc(const _Function &_Func)
Definition: concrt.h:4361
template<typename _Function >
void Concurrency::task_group::run ( const _Function &  _Func,
location _Placement 
)
inline

Schedules a task on the task_group object. If a task_handle object is passed as a parameter to run, the caller is responsible for managing the lifetime of the task_handle object. The version of the method that takes a reference to a function object as a parameter involves heap allocation inside the runtime which may be perform less well than using the version that takes a reference to a task_handle object. The version which takes the parameter _Placement causes the task to be biased towards executing at the location specified by that parameter.

Template Parameters
_FunctionThe type of the function object that will be invoked to execute the body of the task handle.
Parameters
_FuncA function which will be called to invoke the body of the task. This may be a lambda expression or other object which supports a version of the function call operator with the signature void operator()().
_PlacementA reference to the location where the task represented by the _Func parameter should execute.

The runtime schedules the provided work function to run at a later time, which can be after the calling function returns. This method uses a task_handle object to hold a copy of the provided work function. Therefore, any state changes that occur in a function object that you pass to this method will not appear in your copy of that function object. In addition, make sure that the lifetime of any objects that you pass by pointer or by reference to the work function remain valid until the work function returns.

If the task_group destructs as the result of stack unwinding from an exception, you do not need to guarantee that a call has been made to either the wait or run_and_wait method. In this case, the destructor will appropriately cancel and wait for the task represented by the _Task_handle parameter to complete.

The method throws an invalid_multiple_scheduling exception if the task handle given by the _Task_handle parameter has already been scheduled onto a task group object via the run method and there has been no intervening call to either the wait or run_and_wait method on that task group.

See also
task_group::wait Method, task_group::run_and_wait Method, Task Parallelism, location class
614  {
615  _M_task_collection._Schedule(::Concurrency::details::_UnrealizedChore::_InternalAlloc<task_handle<_Function>, _Function>(_Func), &_Placement);
616  }
::Concurrency::details::_TaskCollection _M_task_collection
Definition: ppl.h:841
_CRTIMP void _Schedule(_UnrealizedChore *_PChore, location *_PLocation)
Schedules a chore that can potentially run in parallel. The chore is pushed onto the associated works...
static _ChoreType * _InternalAlloc(const _Function &_Func)
Definition: concrt.h:4361
template<typename _Function >
void Concurrency::task_group::run ( task_handle< _Function > &  _Task_handle)
inline

Schedules a task on the task_group object. If a task_handle object is passed as a parameter to run, the caller is responsible for managing the lifetime of the task_handle object. The version of the method that takes a reference to a function object as a parameter involves heap allocation inside the runtime which may be perform less well than using the version that takes a reference to a task_handle object. The version which takes the parameter _Placement causes the task to be biased towards executing at the location specified by that parameter.

Template Parameters
_FunctionThe type of the function object that will be invoked to execute the body of the task handle.
Parameters
_Task_handleA handle to the work being scheduled. Note that the caller has responsibility for the lifetime of this object. The runtime will continue to expect it to live until either the wait or run_and_wait method has been called on this task_group object.

The runtime schedules the provided work function to run at a later time, which can be after the calling function returns. This method uses a task_handle object to hold a copy of the provided work function. Therefore, any state changes that occur in a function object that you pass to this method will not appear in your copy of that function object. In addition, make sure that the lifetime of any objects that you pass by pointer or by reference to the work function remain valid until the work function returns.

If the task_group destructs as the result of stack unwinding from an exception, you do not need to guarantee that a call has been made to either the wait or run_and_wait method. In this case, the destructor will appropriately cancel and wait for the task represented by the _Task_handle parameter to complete.

The method throws an invalid_multiple_scheduling exception if the task handle given by the _Task_handle parameter has already been scheduled onto a task group object via the run method and there has been no intervening call to either the wait or run_and_wait method on that task group.

See also
task_group::wait Method, task_group::run_and_wait Method, Task Parallelism, location class
653  {
654  _Task_handle._SetRuntimeOwnsLifetime(false);
655  _M_task_collection._Schedule(&_Task_handle);
656  }
::Concurrency::details::_TaskCollection _M_task_collection
Definition: ppl.h:841
_CRTIMP void _Schedule(_UnrealizedChore *_PChore, location *_PLocation)
Schedules a chore that can potentially run in parallel. The chore is pushed onto the associated works...
template<typename _Function >
void Concurrency::task_group::run ( task_handle< _Function > &  _Task_handle,
location _Placement 
)
inline

Schedules a task on the task_group object. If a task_handle object is passed as a parameter to run, the caller is responsible for managing the lifetime of the task_handle object. The version of the method that takes a reference to a function object as a parameter involves heap allocation inside the runtime which may be perform less well than using the version that takes a reference to a task_handle object. The version which takes the parameter _Placement causes the task to be biased towards executing at the location specified by that parameter.

Template Parameters
_FunctionThe type of the function object that will be invoked to execute the body of the task handle.
Parameters
_Task_handleA handle to the work being scheduled. Note that the caller has responsibility for the lifetime of this object. The runtime will continue to expect it to live until either the wait or run_and_wait method has been called on this task_group object.
_PlacementA reference to the location where the task represented by the _Func parameter should execute.

The runtime schedules the provided work function to run at a later time, which can be after the calling function returns. This method uses a task_handle object to hold a copy of the provided work function. Therefore, any state changes that occur in a function object that you pass to this method will not appear in your copy of that function object. In addition, make sure that the lifetime of any objects that you pass by pointer or by reference to the work function remain valid until the work function returns.

If the task_group destructs as the result of stack unwinding from an exception, you do not need to guarantee that a call has been made to either the wait or run_and_wait method. In this case, the destructor will appropriately cancel and wait for the task represented by the _Task_handle parameter to complete.

The method throws an invalid_multiple_scheduling exception if the task handle given by the _Task_handle parameter has already been scheduled onto a task group object via the run method and there has been no intervening call to either the wait or run_and_wait method on that task group.

See also
task_group::wait Method, task_group::run_and_wait Method, Task Parallelism, location class
696  {
697  _Task_handle._SetRuntimeOwnsLifetime(false);
698  _M_task_collection._Schedule(&_Task_handle, &_Placement);
699  }
::Concurrency::details::_TaskCollection _M_task_collection
Definition: ppl.h:841
_CRTIMP void _Schedule(_UnrealizedChore *_PChore, location *_PLocation)
Schedules a chore that can potentially run in parallel. The chore is pushed onto the associated works...
template<class _Function >
void Concurrency::task_group::run ( task_handle< _Function > &&  _Task_handle)
private
template<class _Function >
task_group_status Concurrency::task_group::run_and_wait ( task_handle< _Function > &  _Task_handle)
inline

Schedules a task to be run inline on the calling context with the assistance of the task_group object for full cancellation support. The function then waits until all work on the task_group object has either completed or been canceled. If a task_handle object is passed as a parameter to run_and_wait, the caller is responsible for managing the lifetime of the task_handle object.

Template Parameters
_FunctionThe type of the function object that will be invoked to execute the body of the task handle.
Parameters
_Task_handleA handle to the task which will be run inline on the calling context. Note that the caller has responsibility for the lifetime of this object. The runtime will continue to expect it to live until the run_and_wait method finishes execution.
Returns
An indication of whether the wait was satisfied or the task group was canceled, due to either an explicit cancel operation or an exception being thrown from one of its tasks. For more information, see task_group_status.

Note that one or more of the tasks scheduled to this task_group object may execute inline on the calling context.

If one or more of the tasks scheduled to this task_group object throws an exception, the runtime will select one such exception of its choosing and propagate it out of the call to the run_and_wait method.

Upon return from the run_and_wait method on a task_group object, the runtime resets the object to a clean state where it can be reused. This includes the case where the task_group object was canceled.

In the non-exceptional path of execution, you have a mandate to call either this method or the wait method before the destructor of the task_group executes.

See also
task_group::run Method, task_group::wait Method, Task Parallelism
757  {
758  //
759  // The underlying scheduler's definitions map exactly to the PPL's. No translation beyond the cast is necessary.
760  //
761  _Task_handle._SetRuntimeOwnsLifetime(false);
762  return (task_group_status)_M_task_collection._RunAndWait(&_Task_handle);
763  }
::Concurrency::details::_TaskCollection _M_task_collection
Definition: ppl.h:841
_CRTIMP _TaskCollectionStatus __stdcall _RunAndWait(_UnrealizedChore *_PChore=NULL)
A cancellation friendly wrapper with which to execute _PChore and then waits for all chores running i...
task_group_status
Describes the execution status of a task_group or structured_task_group object. A value of this type ...
Definition: pplinterface.h:114
template<class _Function >
task_group_status Concurrency::task_group::run_and_wait ( const _Function &  _Func)
inline

Schedules a task to be run inline on the calling context with the assistance of the task_group object for full cancellation support. The function then waits until all work on the task_group object has either completed or been canceled. If a task_handle object is passed as a parameter to run_and_wait, the caller is responsible for managing the lifetime of the task_handle object.

Template Parameters
_FunctionThe type of the function object that will be invoked to execute the body of the task.
Parameters
_FuncA function which will be called to invoke the body of the work. This may be a lambda expression or other object which supports a version of the function call operator with the signature void operator()().
Returns
An indication of whether the wait was satisfied or the task group was canceled, due to either an explicit cancel operation or an exception being thrown from one of its tasks. For more information, see task_group_status.

Note that one or more of the tasks scheduled to this task_group object may execute inline on the calling context.

If one or more of the tasks scheduled to this task_group object throws an exception, the runtime will select one such exception of its choosing and propagate it out of the call to the run_and_wait method.

Upon return from the run_and_wait method on a task_group object, the runtime resets the object to a clean state where it can be reused. This includes the case where the task_group object was canceled.

In the non-exceptional path of execution, you have a mandate to call either this method or the wait method before the destructor of the task_group executes.

See also
task_group::run Method, task_group::wait Method, Task Parallelism
796  {
797  //
798  // The underlying scheduler's definitions map exactly to the PPL's. No translation beyond the cast is necessary.
799  //
801  }
::Concurrency::details::_TaskCollection _M_task_collection
Definition: ppl.h:841
_CRTIMP _TaskCollectionStatus __stdcall _RunAndWait(_UnrealizedChore *_PChore=NULL)
A cancellation friendly wrapper with which to execute _PChore and then waits for all chores running i...
task_group_status
Describes the execution status of a task_group or structured_task_group object. A value of this type ...
Definition: pplinterface.h:114
static _ChoreType * _InternalAlloc(const _Function &_Func)
Definition: concrt.h:4361
task_group_status Concurrency::task_group::wait ( )
inline

Waits until all work on the task_group object has either completed or been canceled.

Returns
An indication of whether the wait was satisfied or the task group was canceled, due to either an explicit cancel operation or an exception being thrown from one of its tasks. For more information, see task_group_status.

Note that one or more of the tasks scheduled to this task_group object may execute inline on the calling context.

If one or more of the tasks scheduled to this task_group object throws an exception, the runtime will select one such exception of its choosing and propagate it out of the call to the wait method.

Calling wait on a task_group object resets it to a clean state where it can be reused. This includes the case where the task_group object was canceled.

In the non-exceptional path of execution, you have a mandate to call either this method or the run_and_wait method before the destructor of the task_group executes.

719  {
720  //
721  // The underlying scheduler's definitions map exactly to the PPL's. No translation beyond the cast is necessary.
722  //
723  return static_cast<task_group_status>(_M_task_collection._Wait());
724  }
::Concurrency::details::_TaskCollection _M_task_collection
Definition: ppl.h:841
_TaskCollectionStatus _Wait()
Waits for all chores running in the _TaskCollection to finish (normally or abnormally). This method encapsulates all the running tasks in an exception handling block, and will re-throw any exceptions that occur in any of it tasks (if those exceptions occur on another thread, they are marshaled from that thread to the thread where the _TaskCollection was created, and re-thrown). After this function returns, the _TaskCollection cannot be used for scheduling further work.
Definition: concrt.h:4930
task_group_status
Describes the execution status of a task_group or structured_task_group object. A value of this type ...
Definition: pplinterface.h:114

Member Data Documentation

::Concurrency::details::_TaskCollection Concurrency::task_group::_M_task_collection
private

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