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...
 
void _ClearStoredException () const
 Internal method that observe and clear the exception stored in the task completion event. This is used internally by when_any. 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 _Ty >
class task
 
template<typename _Ty >
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.

2347  : _M_Impl(std::make_shared<details::_Task_completion_event_impl<_ResultType>>())
2348  {
2349  }
shared_ptr< _Ty > make_shared(_Types &&..._Args)
Definition: memory:968
std::shared_ptr< details::_Task_completion_event_impl< _ResultType > > _M_Impl
Definition: ppltasks.h:2607

Member Function Documentation

template<typename _ResultType>
template<typename _E >
Concurrency::task_completion_event< _ResultType >::__declspec ( noinline  ) const
inline
2415  {
2416  // It is important that _CAPTURE_CALLSTACK() evaluates to the instruction after the call instruction for set_exception.
2418  }
exception_ptr make_exception_ptr(_Ex _Except) _NOEXCEPT
Definition: exception:371
#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
bool _Cancel() const
Internal method to cancel the task_completion_event. Any task created using this event will be marked...
Definition: ppltasks.h:2438
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.

2429  {
2430  // It is important that _CAPTURE_CALLSTACK() evaluates to the instruction after the call instruction for set_exception.
2431  return _Cancel(_ExceptionPtr, _CAPTURE_CALLSTACK());
2432  }
#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
bool _Cancel() const
Internal method to cancel the task_completion_event. Any task created using this event will be marked...
Definition: ppltasks.h:2438
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.

2439  {
2440  // Cancel with the stored exception if one exists.
2441  return _CancelInternal();
2442  }
bool _CancelInternal() const
Cancels the task_completion_event.
Definition: ppltasks.h:2525
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.

2450  {
2451  bool _Canceled;
2452  if(_StoreException(_ExHolder, _SetExceptionAddressHint))
2453  {
2454  _Canceled = _CancelInternal();
2455  _ASSERTE(_Canceled);
2456  }
2457  else
2458  {
2459  _Canceled = false;
2460  }
2461  return _Canceled;
2462  }
Definition: concrt.h:4305
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:2470
bool _CancelInternal() const
Cancels the task_completion_event.
Definition: ppltasks.h:2525
template<typename _ResultType>
bool Concurrency::task_completion_event< _ResultType >::_CancelInternal ( ) const
inlineprivate

Cancels the task_completion_event.

2526  {
2527  // Cancellation of task completion events is an internal only utility. Our usage is such that _CancelInternal
2528  // will never be invoked if the task completion event has been set.
2529  _ASSERTE(!_M_Impl->_M_fHasValue);
2530  if (_M_Impl->_M_fIsCanceled)
2531  {
2532  return false;
2533  }
2534 
2535  _TaskList _Tasks;
2536  bool _Cancel = false;
2537  {
2538  ::std::lock_guard<std::mutex> _LockHolder(_M_Impl->_M_taskListCritSec);
2539  _ASSERTE(!_M_Impl->_M_fHasValue);
2540  if (!_M_Impl->_M_fIsCanceled)
2541  {
2542  _M_Impl->_M_fIsCanceled = true;
2543  _Tasks.swap(_M_Impl->_M_tasks);
2544  _Cancel = true;
2545  }
2546  }
2547 
2548  bool _UserException = _M_Impl->_HasUserException();
2549 
2550  if (_Cancel)
2551  {
2552  for( auto _TaskIt = _Tasks.begin(); _TaskIt != _Tasks.end(); ++_TaskIt )
2553  {
2554  // Need to call this after the lock is released. See comments in set().
2555  if (_UserException)
2556  {
2557  (*_TaskIt)->_CancelWithExceptionHolder(_M_Impl->_M_exceptionHolder, true);
2558  }
2559  else
2560  {
2561  (*_TaskIt)->_Cancel(true);
2562  }
2563  }
2564  }
2565  return _Cancel;
2566  }
details::_Task_completion_event_impl< _ResultType >::_TaskList _TaskList
Definition: ppltasks.h:2520
std::shared_ptr< details::_Task_completion_event_impl< _ResultType > > _M_Impl
Definition: ppltasks.h:2607
bool _Cancel() const
Internal method to cancel the task_completion_event. Any task created using this event will be marked...
Definition: ppltasks.h:2438
template<typename _ResultType>
void Concurrency::task_completion_event< _ResultType >::_ClearStoredException ( ) const
inline

Internal method that observe and clear the exception stored in the task completion event. This is used internally by when_any.

2487  {
2488  ::std::lock_guard<std::mutex> _LockHolder(_M_Impl->_M_taskListCritSec);
2489  if (_M_Impl->_M_exceptionHolder)
2490  {
2491  details::atomic_exchange(_M_Impl->_M_exceptionHolder->_M_exceptionObserved, 1l);
2492  _M_Impl->_M_exceptionHolder.reset();
2493  }
2494  }
_T atomic_exchange(std::atomic< _T > &_Target, _T _Value)
Definition: pplinterface.h:246
std::shared_ptr< details::_Task_completion_event_impl< _ResultType > > _M_Impl
Definition: ppltasks.h:2607
template<typename _ResultType>
bool Concurrency::task_completion_event< _ResultType >::_IsTriggered ( ) const
inline

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

2500  {
2501  return _M_Impl->_M_fHasValue || _M_Impl->_M_fIsCanceled;
2502  }
std::shared_ptr< details::_Task_completion_event_impl< _ResultType > > _M_Impl
Definition: ppltasks.h:2607
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.

2573  {
2574  enum { _Nothing, _Trigger, _Cancel } _Action = _Nothing;
2575  {
2576  ::std::lock_guard<std::mutex> _LockHolder(_M_Impl->_M_taskListCritSec);
2577 
2578  //If an exception was already set on this event, then cancel the task with the stored exception.
2579  if (_M_Impl->_HasUserException())
2580  {
2581  _Action = _Cancel;
2582  }
2583  else if (_M_Impl->_M_fHasValue)
2584  {
2585  _Action = _Trigger;
2586  }
2587  else
2588  {
2589  _M_Impl->_M_tasks.push_back(_TaskParam);
2590  }
2591  }
2592 
2593  switch (_Action)
2594  {
2595  case _Trigger:
2596  _TaskParam->_FinalizeAndRunContinuations(_M_Impl->_M_value.Get());
2597  break;
2598  case _Cancel:
2599  _TaskParam->_CancelWithExceptionHolder(_M_Impl->_M_exceptionHolder, true);
2600  break;
2601  case _Nothing:
2602  default:
2603  break;
2604  }
2605  }
std::shared_ptr< details::_Task_completion_event_impl< _ResultType > > _M_Impl
Definition: ppltasks.h:2607
bool _Cancel() const
Internal method to cancel the task_completion_event. Any task created using this event will be marked...
Definition: ppltasks.h:2438
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.

2471  {
2472  ::std::lock_guard<std::mutex> _LockHolder(_M_Impl->_M_taskListCritSec);
2473  if (!_IsTriggered() && !_M_Impl->_HasUserException())
2474  {
2475  // Create the exception holder only if we have ensured there we will be successful in setting it onto the
2476  // task completion event. Failing to do so will result in an unobserved task exception.
2477  _M_Impl->_M_exceptionHolder = _ToExceptionHolder(_ExHolder, _SetExceptionAddressHint);
2478  return true;
2479  }
2480  return false;
2481  }
bool _IsTriggered() const
Tests whether current event has been either Set, or Canceled.
Definition: ppltasks.h:2499
static std::shared_ptr< details::_ExceptionHolder > _ToExceptionHolder(const std::shared_ptr< details::_ExceptionHolder > &_ExHolder, const details::_TaskCreationCallstack &)
Definition: ppltasks.h:2506
std::shared_ptr< details::_Task_completion_event_impl< _ResultType > > _M_Impl
Definition: ppltasks.h:2607
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
2507  {
2508  return _ExHolder;
2509  }
template<typename _ResultType>
static std::shared_ptr<details::_ExceptionHolder> Concurrency::task_completion_event< _ResultType >::_ToExceptionHolder ( std::exception_ptr  _ExceptionPtr,
const details::_TaskCreationCallstack _SetExceptionAddressHint 
)
inlinestaticprivate
2512  {
2513  return std::make_shared<details::_ExceptionHolder>(_ExceptionPtr, _SetExceptionAddressHint);
2514  }
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.

2368  {
2369  // Subsequent sets are ignored. This makes races to set benign: the first setter wins and all others are ignored.
2370  if (_IsTriggered())
2371  {
2372  return false;
2373  }
2374 
2375  _TaskList _Tasks;
2376  bool _RunContinuations = false;
2377  {
2378  ::std::lock_guard<std::mutex> _LockHolder(_M_Impl->_M_taskListCritSec);
2379 
2380  if (!_IsTriggered())
2381  {
2382  _M_Impl->_M_value.Set(_Result);
2383  _M_Impl->_M_fHasValue = true;
2384 
2385  _Tasks.swap(_M_Impl->_M_tasks);
2386  _RunContinuations = true;
2387  }
2388  }
2389 
2390  if (_RunContinuations)
2391  {
2392  for( auto _TaskIt = _Tasks.begin(); _TaskIt != _Tasks.end(); ++_TaskIt )
2393  {
2394  // If current task was cancelled by a cancellation_token, it would be in cancel pending state.
2395  if ((*_TaskIt)->_IsPendingCancel())
2396  (*_TaskIt)->_Cancel(true);
2397  else
2398  {
2399  // Tasks created with task_completion_events can be marked as async, (we do this in when_any and when_all
2400  // if one of the tasks involved is an async task). Since continuations of async tasks can execute inline, we
2401  // need to run continuations after the lock is released.
2402  (*_TaskIt)->_FinalizeAndRunContinuations(_M_Impl->_M_value.Get());
2403  }
2404  }
2405 
2406  return true;
2407  }
2408 
2409  return false;
2410  }
bool _IsTriggered() const
Tests whether current event has been either Set, or Canceled.
Definition: ppltasks.h:2499
details::_Task_completion_event_impl< _ResultType >::_TaskList _TaskList
Definition: ppltasks.h:2520
std::shared_ptr< details::_Task_completion_event_impl< _ResultType > > _M_Impl
Definition: ppltasks.h:2607

Friends And Related Function Documentation

template<typename _ResultType>
template<typename _Ty >
friend class task
friend
template<typename _ResultType>
template<typename _Ty >
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: