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

The task_completion_event class allows you to delay the execution of a task until a condition is satisfied, or start a task in response to an external event. More...

#include <ppltasks.h>

Public Member Functions

 task_completion_event ()
 Constructs a task_completion_event object. More...
 
bool set (_ResultType _Result) const
 Sets the task completion event. More...
 
template<typename _E >
 __declspec (noinline) bool set_exception(_E _Except) const
 
 __declspec (noinline) bool set_exception(std
 Propagates an exception to all tasks associated with this event. More...
 
bool _Cancel () const
 Internal method to cancel the task_completion_event. Any task created using this event will be marked as canceled if it has not already been set. More...
 
template<typename _ExHolderType >
bool _Cancel (_ExHolderType _ExHolder, const details::_TaskCreationCallstack &_SetExceptionAddressHint=details::_TaskCreationCallstack()) const
 Internal method to cancel the task_completion_event with the exception provided. Any task created using this event will be canceled with the same exception. More...
 
template<typename _ExHolderType >
bool _StoreException (_ExHolderType _ExHolder, const details::_TaskCreationCallstack &_SetExceptionAddressHint=details::_TaskCreationCallstack()) const
 Internal method that stores an exception in the task completion event. This is used internally by when_any. Note, this does not cancel the task completion event. A task completion event with a stored exception can bet set() successfully. If it is canceled, it will cancel with the stored exception, if one is present. More...
 
bool _IsTriggered () const
 Tests whether current event has been either Set, or Canceled. More...
 

Private Types

typedef details::_Task_completion_event_impl< _ResultType >::_TaskList _TaskList
 

Private Member Functions

bool _CancelInternal () const
 Cancels the task_completion_event. More...
 
void _RegisterTask (const typename details::_Task_ptr< _ResultType >::_Type &_TaskParam)
 Register a task with this event. This function is called when a task is constructed using a task_completion_event. More...
 

Static Private Member Functions

static std::shared_ptr< details::_ExceptionHolder_ToExceptionHolder (const std::shared_ptr< details::_ExceptionHolder > &_ExHolder, const details::_TaskCreationCallstack &)
 
static std::shared_ptr< details::_ExceptionHolder_ToExceptionHolder (std::exception_ptr _ExceptionPtr, const details::_TaskCreationCallstack &_SetExceptionAddressHint)
 

Private Attributes

std::shared_ptr< details::_Task_completion_event_impl< _ResultType > > _M_Impl
 

Friends

template<typename T >
class task
 
template<typename T >
class task_completion_event
 

Detailed Description

template<typename _ResultType>
class Concurrency::task_completion_event< _ResultType >

The task_completion_event class allows you to delay the execution of a task until a condition is satisfied, or start a task in response to an external event.

Template Parameters
_ResultTypeThe result type of this task_completion_event class.

Use a task created from a task completion event when your scenario requires you to create a task that will complete, and thereby have its continuations scheduled for execution, at some point in the future. The task_completion_event must have the same type as the task you create, and calling the set method on the task completion event with a value of that type will cause the associated task to complete, and provide that value as a result to its continuations.

If the task completion event is never signaled, any tasks created from it will be canceled when it is destructed.

task_completion_event behaves like a smart pointer, and should be passed by value.

See also
task Class

Member Typedef Documentation

template<typename _ResultType>
typedef details::_Task_completion_event_impl<_ResultType>::_TaskList Concurrency::task_completion_event< _ResultType >::_TaskList
private

Constructor & Destructor Documentation

template<typename _ResultType>
Concurrency::task_completion_event< _ResultType >::task_completion_event ( )
inline

Constructs a task_completion_event object.

2631  : _M_Impl(std::make_shared<details::_Task_completion_event_impl<_ResultType>>())
2632  {
2633  }
shared_ptr< _Ty > make_shared(_Types &&..._Args)
Definition: memory:1000
std::shared_ptr< details::_Task_completion_event_impl< _ResultType > > _M_Impl
Definition: ppltasks.h:2865

Member Function Documentation

template<typename _ResultType>
template<typename _E >
Concurrency::task_completion_event< _ResultType >::__declspec ( noinline  ) const
inline
2702  {
2703  // It is important that _CAPTURE_CALLSTACK() evaluate to the instruction after the call instruction for set_exception.
2705  }
#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:162
bool _Cancel() const
Internal method to cancel the task_completion_event. Any task created using this event will be marked...
Definition: ppltasks.h:2725
exception_ptr make_exception_ptr(_E _Except)
Definition: exception:539
template<typename _ResultType>
Concurrency::task_completion_event< _ResultType >::__declspec ( noinline  )
inline

Propagates an exception to all tasks associated with this event.

The exception_ptr that indicates the exception to set this event with.

2716  {
2717  // It is important that _CAPTURE_CALLSTACK() evaluate to the instruction after the call instruction for set_exception.
2718  return _Cancel(_ExceptionPtr, _CAPTURE_CALLSTACK());
2719  }
#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:162
bool _Cancel() const
Internal method to cancel the task_completion_event. Any task created using this event will be marked...
Definition: ppltasks.h:2725
template<typename _ResultType>
bool Concurrency::task_completion_event< _ResultType >::_Cancel ( ) const
inline

Internal method to cancel the task_completion_event. Any task created using this event will be marked as canceled if it has not already been set.

2726  {
2727  // Cancel with the stored exception if one exists.
2728  return _CancelInternal();
2729  }
bool _CancelInternal() const
Cancels the task_completion_event.
Definition: ppltasks.h:2799
template<typename _ResultType>
template<typename _ExHolderType >
bool Concurrency::task_completion_event< _ResultType >::_Cancel ( _ExHolderType  _ExHolder,
const details::_TaskCreationCallstack _SetExceptionAddressHint = details::_TaskCreationCallstack () 
) const
inline

Internal method to cancel the task_completion_event with the exception provided. Any task created using this event will be canceled with the same exception.

2737  {
2738  bool _Canceled;
2739  if(_StoreException(_ExHolder, _SetExceptionAddressHint))
2740  {
2741  _Canceled = _CancelInternal();
2742  _ASSERTE(_Canceled);
2743  }
2744  else
2745  {
2746  _Canceled = false;
2747  }
2748  return _Canceled;
2749  }
Definition: concrt.h:4427
#define _ASSERTE(expr)
Definition: crtdbg.h:216
bool _StoreException(_ExHolderType _ExHolder, const details::_TaskCreationCallstack &_SetExceptionAddressHint=details::_TaskCreationCallstack()) const
Internal method that stores an exception in the task completion event. This is used internally by whe...
Definition: ppltasks.h:2757
bool _CancelInternal() const
Cancels the task_completion_event.
Definition: ppltasks.h:2799
template<typename _ResultType>
bool Concurrency::task_completion_event< _ResultType >::_CancelInternal ( ) const
inlineprivate

Cancels the task_completion_event.

2800  {
2801  // Cancellation of task completion events is an internal only utility. Our usage is such that _CancelInternal
2802  // will never be invoked if the task completion event has been set.
2803  _ASSERTE(!_M_Impl->_M_fHasValue);
2804  if (_M_Impl->_M_fIsCanceled)
2805  {
2806  return false;
2807  }
2808 
2809  _TaskList _Tasks;
2810  bool _Cancel = false;
2811  {
2812  ::Concurrency::extensibility::scoped_critical_section_t _LockHolder(_M_Impl->_M_taskListCritSec);
2813  _ASSERTE(!_M_Impl->_M_fHasValue);
2814  if (!_M_Impl->_M_fIsCanceled)
2815  {
2816  _M_Impl->_M_fIsCanceled = true;
2817  _Tasks.swap(_M_Impl->_M_tasks);
2818  _Cancel = true;
2819  }
2820  }
2821 
2822  bool _UserException = _M_Impl->_HasUserException();
2823 
2824  if (_Cancel)
2825  {
2826  for( auto _TaskIt = _Tasks.begin(); _TaskIt != _Tasks.end(); ++_TaskIt )
2827  {
2828  // Need to call this after the lock is released. See comments in set().
2829  if (_UserException)
2830  {
2831  (*_TaskIt)->_CancelWithExceptionHolder(_M_Impl->_M_exceptionHolder, true);
2832  }
2833  else
2834  {
2835  (*_TaskIt)->_Cancel(true);
2836  }
2837  }
2838  }
2839  return _Cancel;
2840  }
#define _ASSERTE(expr)
Definition: crtdbg.h:216
details::_Task_completion_event_impl< _ResultType >::_TaskList _TaskList
Definition: ppltasks.h:2794
std::shared_ptr< details::_Task_completion_event_impl< _ResultType > > _M_Impl
Definition: ppltasks.h:2865
bool _Cancel() const
Internal method to cancel the task_completion_event. Any task created using this event will be marked...
Definition: ppltasks.h:2725
An exception safe RAII wrapper for a critical_section object.
Definition: concrt.h:3661
template<typename _ResultType>
bool Concurrency::task_completion_event< _ResultType >::_IsTriggered ( ) const
inline

Tests whether current event has been either Set, or Canceled.

2774  {
2775  return _M_Impl->_M_fHasValue || _M_Impl->_M_fIsCanceled;
2776  }
std::shared_ptr< details::_Task_completion_event_impl< _ResultType > > _M_Impl
Definition: ppltasks.h:2865
template<typename _ResultType>
void Concurrency::task_completion_event< _ResultType >::_RegisterTask ( const typename details::_Task_ptr< _ResultType >::_Type _TaskParam)
inlineprivate

Register a task with this event. This function is called when a task is constructed using a task_completion_event.

2847  {
2848  ::Concurrency::extensibility::scoped_critical_section_t _LockHolder(_M_Impl->_M_taskListCritSec);
2849 
2850  //If an exception was already set on this event, then cancel the task with the stored exception.
2851  if(_M_Impl->_HasUserException())
2852  {
2853  _TaskParam->_CancelWithExceptionHolder(_M_Impl->_M_exceptionHolder, true);
2854  }
2855  else if (_M_Impl->_M_fHasValue)
2856  {
2857  _TaskParam->_FinalizeAndRunContinuations(_M_Impl->_M_value.Get());
2858  }
2859  else
2860  {
2861  _M_Impl->_M_tasks.push_back(_TaskParam);
2862  }
2863  }
std::shared_ptr< details::_Task_completion_event_impl< _ResultType > > _M_Impl
Definition: ppltasks.h:2865
An exception safe RAII wrapper for a critical_section object.
Definition: concrt.h:3661
template<typename _ResultType>
template<typename _ExHolderType >
bool Concurrency::task_completion_event< _ResultType >::_StoreException ( _ExHolderType  _ExHolder,
const details::_TaskCreationCallstack _SetExceptionAddressHint = details::_TaskCreationCallstack () 
) const
inline

Internal method that stores an exception in the task completion event. This is used internally by when_any. Note, this does not cancel the task completion event. A task completion event with a stored exception can bet set() successfully. If it is canceled, it will cancel with the stored exception, if one is present.

2758  {
2759  ::Concurrency::extensibility::scoped_critical_section_t _LockHolder(_M_Impl->_M_taskListCritSec);
2760  if (!_IsTriggered() && !_M_Impl->_HasUserException())
2761  {
2762  // Create the exception holder only if we have ensured there we will be successful in setting it onto the
2763  // task completion event. Failing to do so will result in an unobserved task exception.
2764  _M_Impl->_M_exceptionHolder = _ToExceptionHolder(_ExHolder, _SetExceptionAddressHint);
2765  return true;
2766  }
2767  return false;
2768  }
bool _IsTriggered() const
Tests whether current event has been either Set, or Canceled.
Definition: ppltasks.h:2773
static std::shared_ptr< details::_ExceptionHolder > _ToExceptionHolder(const std::shared_ptr< details::_ExceptionHolder > &_ExHolder, const details::_TaskCreationCallstack &)
Definition: ppltasks.h:2780
std::shared_ptr< details::_Task_completion_event_impl< _ResultType > > _M_Impl
Definition: ppltasks.h:2865
An exception safe RAII wrapper for a critical_section object.
Definition: concrt.h:3661
template<typename _ResultType>
static std::shared_ptr<details::_ExceptionHolder> Concurrency::task_completion_event< _ResultType >::_ToExceptionHolder ( const std::shared_ptr< details::_ExceptionHolder > &  _ExHolder,
const details::_TaskCreationCallstack  
)
inlinestaticprivate
2781  {
2782  return _ExHolder;
2783  }
template<typename _ResultType>
static std::shared_ptr<details::_ExceptionHolder> Concurrency::task_completion_event< _ResultType >::_ToExceptionHolder ( std::exception_ptr  _ExceptionPtr,
const details::_TaskCreationCallstack _SetExceptionAddressHint 
)
inlinestaticprivate
2786  {
2787  return std::make_shared<details::_ExceptionHolder>(_ExceptionPtr, _SetExceptionAddressHint);
2788  }
template<typename _ResultType>
bool Concurrency::task_completion_event< _ResultType >::set ( _ResultType  _Result) const
inline

Sets the task completion event.

Parameters
_ResultThe result to set this event with.
Returns
The method returns true if it was successful in setting the event. It returns false if the event is already set.

In the presence of multiple or concurrent calls to set, only the first call will succeed and its result (if any) will be stored in the task completion event. The remaining sets are ignored and the method will return false. When you set a task completion event, all the tasks created from that event will immediately complete, and its continuations, if any, will be scheduled. Task completion objects that have a _ResultType other than void will pass the value to their continuations.

2652  {
2653  // Subsequent sets are ignored. This makes races to set benign: the first setter wins and all others are ignored.
2654  if (_IsTriggered())
2655  {
2656  return false;
2657  }
2658 
2659  _TaskList _Tasks;
2660  bool _RunContinuations = false;
2661  {
2662  ::Concurrency::extensibility::scoped_critical_section_t _LockHolder(_M_Impl->_M_taskListCritSec);
2663 
2664  if (!_IsTriggered())
2665  {
2666  _M_Impl->_M_value.Set(_Result);
2667  _M_Impl->_M_fHasValue = true;
2668 
2669  _Tasks.swap(_M_Impl->_M_tasks);
2670  _RunContinuations = true;
2671  }
2672  }
2673 
2674  if (_RunContinuations)
2675  {
2676  for( auto _TaskIt = _Tasks.begin(); _TaskIt != _Tasks.end(); ++_TaskIt )
2677  {
2678  // If current task was cancelled by a cancellation_token, it would be in cancel pending state.
2679  if ((*_TaskIt)->_IsPendingCancel())
2680  (*_TaskIt)->_Cancel(true);
2681  else
2682  {
2683  // Tasks created with task_completion_events can be marked as async, (we do this in when_any and when_all
2684  // if one of the tasks involved is an async task). Since continuations of async tasks can execute inline, we
2685  // need to run continuations after the lock is released.
2686  (*_TaskIt)->_FinalizeAndRunContinuations(_M_Impl->_M_value.Get());
2687  }
2688  }
2689  if (_M_Impl->_HasUserException())
2690  {
2691  _M_Impl->_M_exceptionHolder.reset();
2692  }
2693  return true;
2694  }
2695 
2696  return false;
2697  }
bool _IsTriggered() const
Tests whether current event has been either Set, or Canceled.
Definition: ppltasks.h:2773
details::_Task_completion_event_impl< _ResultType >::_TaskList _TaskList
Definition: ppltasks.h:2794
std::shared_ptr< details::_Task_completion_event_impl< _ResultType > > _M_Impl
Definition: ppltasks.h:2865
An exception safe RAII wrapper for a critical_section object.
Definition: concrt.h:3661

Friends And Related Function Documentation

template<typename _ResultType>
template<typename T >
friend class task
friend
template<typename _ResultType>
template<typename T >
friend class task_completion_event
friend

Member Data Documentation

template<typename _ResultType>
std::shared_ptr<details::_Task_completion_event_impl<_ResultType> > Concurrency::task_completion_event< _ResultType >::_M_Impl
private

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