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.

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

Member Function Documentation

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

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

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

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

Cancels the task_completion_event.

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

2488  {
2489  ::std::lock_guard<std::mutex> _LockHolder(_M_Impl->_M_taskListCritSec);
2490  if (_M_Impl->_M_exceptionHolder)
2491  {
2492  details::atomic_exchange(_M_Impl->_M_exceptionHolder->_M_exceptionObserved, 1l);
2493  _M_Impl->_M_exceptionHolder.reset();
2494  }
2495  }
_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:2608
template<typename _ResultType>
bool Concurrency::task_completion_event< _ResultType >::_IsTriggered ( ) const
inline

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

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

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

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

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

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: