You.i Engine
CYIFuture< ResultType > Class Template Reference

Detailed Description

template<typename ResultType = void>
class CYIFuture< ResultType >

A CYIFuture object is a container for a value that will be assigned at a future time, typically from a different thread. The templated type of this class is the type of value that can be stored in this object.

Functions are provided to allow a user to get the result of the content of the CYIFuture. If no value has been assigned yet, the user's thread will block until a value is assigned, or until a user-specified timeout is reached. Once a value has been assigned, the getter functions return immediately.

A value can only be assigned to a CYIFuture once. Further attempts to assign a value will return failure and will have no effect.

When used with pointer types, CYIFuture objects takes ownership of pointers assigned to them. When the future is deleted, the contained object is deleted as well. This can be prevented by calling Take() to take ownership of the contained object.

Signals can optionally be connected so that users are notified when a value is assigned to the CYIFuture. When used with an associated CYITask, additionnal signals are available to notify when the task's execution has started or when the task has been cancelled.

A CYIFuture object can optionally be associated with a CYITask object. This association is done through the CYITask itself. Only one task can be associated with a CYIFuture object, and that link cannot be broken once it has been made. When used with an associated task, cancelling a future will result in the associated task being cancelled as well (unless it has already started executing). Deleting a CYIFuture object will automatically result in an associated task to be cancelled.

All public functions, with the exception of the destructor, are thread-safe.

Template Parameters
ResultTypethe type of object that can be assigned to this Future object.
See also
CYITask<ResultType>
CYIAbstractFuture

#include <thread/YiFuture.h>

Inheritance diagram for CYIFuture< ResultType >:

Public Member Functions

 CYIFuture ()
 
virtual ~CYIFuture ()
 
bool Get (ResultType *pValue) const
 
const ResultType & Get () const
 
bool Get (ResultType *pValue, uint32_t uTimeoutMs) const
 
const ResultType & Get (uint32_t uTimeoutMs) const
 
bool Set (const ResultType &rValue)
 
- Public Member Functions inherited from CYIAbstractFuture
virtual ~CYIAbstractFuture ()
 
bool IsCancelled () const
 
bool IsCompleted () const
 
bool Cancel ()
 
bool CancelOrWait ()
 
bool Wait () const
 
bool Wait (uint64_t uTimeoutMs) const
 

Public Attributes

CYISignal< const ResultType & > Completed
 A signal triggered when a value is assigned to this object. More...
 
- Public Attributes inherited from CYIAbstractFuture
CYISignal ExecutionStarted
 A signal triggered when an associated task begins execution. More...
 
CYISignal Cancelled
 A signal triggered when this object is cancelled. More...
 

Protected Member Functions

bool SetCompleted (const ResultType &rValue)
 Equivalent to CYIFuture<ResultType>::Set(), but requires that m_mutex be locked prior to calling this function. More...
 
- Protected Member Functions inherited from CYIAbstractFuture
void ExclusiveLock () const
 Locks both this object and the associated task (if it exists). More...
 
void ExclusiveUnlock () const
 Unlocks both this object and the associated task (if it exists). More...
 
void NotifyStateChanged () const
 Notify waiters (e.g. threads blocked on calls to Wait, Get or Take) of state change. More...
 
bool SetTask (CYITaskBase *pTask)
 Sets the task asssociated with this object to the provided task. Returns true if the task was associated successfully, false otherwise. Has no effect if a task has already been assigned, or if this future has been cancelled or had a value assigned already. More...
 
void DisconnectFromTask ()
 Removes the task reference, if it exists. If an associated task exists, an attempt is made at cancelling it. More...
 
bool SetCancelledNonLocking ()
 Sets the 'cancelled' flag on this object, if it hasn't been done already and the future hasn't had a value assigned to it. Will awaken all waiters if the cancellation was successful (and will trigger the Cancelled signal). Returns false if the future could not be cancelled. The caller must have m_mutex locked. More...
 
bool WaitNonLocking () const
 Equivalent to the Wait() function, but the caller must have m_mutex locked. More...
 
bool WaitNonLocking (uint64_t uTimeoutMs) const
 Equivalent to the Wait() function, but the caller must have m_mutex locked. More...
 
bool CancelInternal (bool bWait)
 

Friends

template<typename T >
class CYITask
 

Constructor & Destructor Documentation

template<typename ResultType = void>
CYIFuture< ResultType >::CYIFuture ( )
template<typename ResultType = void>
virtual CYIFuture< ResultType >::~CYIFuture ( )
virtual

Member Function Documentation

template<typename ResultType = void>
bool CYIFuture< ResultType >::Get ( ResultType *  pValue) const

Waits until a value has been assigned to this object, or until this object has been cancelled. If a value has been assigned, the value is copied into the provided variable. If the future is cancelled, the specified variable is untouched.

Note
Returns immediately if this object has been assigned a value or has been cancelled.
Warning
If no value gets assigned to this object, this function will wait forever.
Returns
Returns true if a value has been assigned to this object (and to the provided variable).
template<typename ResultType = void>
const ResultType& CYIFuture< ResultType >::Get ( ) const

Waits until a value has been assigned to this object, or until this object has been cancelled. If a value has been assigned, a reference to the value is returned. If the future is cancelled, a value constructed by the default constructor of ResultType is returned.

Note
Returns immediately if this object has been assigned a value or has been cancelled.
Warning
If no value gets assigned to this object, this function will wait forever.
Returns
Returns a copy of the value if a value was assigned to this object, and returns a value constructed by the default constructor of ResultType if the future has been cancelled.
template<typename ResultType = void>
bool CYIFuture< ResultType >::Get ( ResultType *  pValue,
uint32_t  uTimeoutMs 
) const

Waits until a value has been assigned to this object, until this object has been cancelled, or until the specified timeout is reached. If a value has been assigned, a copy of the value is stored in the provided variable. If the future is cancelled or if the timeout value is reached, the provided variable is untouched.

Note
Returns immediately if this object has been assigned a value or has been cancelled.
In some circumstances, this function may return before the timeout is reached, even if this object hasn't been cancelled and hasn't had a value assigned to it (e.g. in case of a 'spurious wakeup').
Returns
Returns true if a value has been assigned to this object (and to the provided variable).
template<typename ResultType = void>
const ResultType& CYIFuture< ResultType >::Get ( uint32_t  uTimeoutMs) const

Waits until a value has been assigned to this object, until this object has been cancelled, or until the specified timeout is reached. If a value has been assigned, a reference to it is returned. If the future is cancelled or if the timeout value is reached, a value constructed by the default constructor of ResultType is returned.

Note
Returns immediately if this object has been assigned a value or has been cancelled.
In some circumstances, this function may return before the timeout is reached, even if this object hasn't been cancelled and hasn't had a value assigned to it (e.g. in case of a 'spurious wakeup').
Returns
Returns a copy of the value if a value was assigned to this object, and returns a value constructed by the default constructor of ResultType if the future has been cancelled or if the timeout value was reached.
template<typename ResultType = void>
bool CYIFuture< ResultType >::Set ( const ResultType &  rValue)

Assigns a value to this object.

A value can only be assigned once, and cannot be done if the future has been cancelled. A successful assignment will result in the Completed signal being triggered as well as threads blocked on calls to Wait and Get to wake up.

Returns
Returns true if the assignement succeeded.
template<typename ResultType = void>
bool CYIFuture< ResultType >::SetCompleted ( const ResultType &  rValue)
protected

Equivalent to CYIFuture<ResultType>::Set(), but requires that m_mutex be locked prior to calling this function.

Friends And Related Function Documentation

template<typename ResultType = void>
template<typename T >
friend class CYITask
friend

Member Data Documentation

template<typename ResultType = void>
CYISignal<const ResultType&> CYIFuture< ResultType >::Completed

A signal triggered when a value is assigned to this object.


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