You.i Engine
CYITask< ResultType > Class Template Referenceabstract

Detailed Description

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

This class defines a task that can be executed. After execution, the task returns a value of type ResultType, which is assigned to an associated CYIFuture object (if it exists).

A future object can be associated with a task in one of two ways. Either a reference to the future is passed to the task upon construction, or the SetFuture function is used to associate a future with the task. A task can only be associated with a single future object, and a future object can only be associated with a single task object. Deleting either the task object or the future object is thread-safe, and will result in the associated object being cancelled (if possible).

If a task has an associated future, the result of the task's execution is assigned to the future object after the task is executed. This will result in the Completed signal of the future object being triggered, and in the waking of any thread that may be blocked on Wait(), Get() or Take() function calls from the future object.

Tasks can be created without an associated future object. In that case, the result of the execution of the task is immediately discarded.

Note
If a task is created with a Future reference and that Future object already has an associated task, the task will be created without an associated Future object and a warning will be logged.
Warning
Task executors (e.g. Thread Pools) typically take ownership of tasks that are enqueued into them. For this reason, users should not retain a pointer to the task as that pointer may become invalid without prior notice.

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

Template Parameters
ResultTypethe type of the execution result of this task.
See also
CYITaskBase
CYIFuture<ResultType>
CYIThreadPool

#include <thread/YiTask.h>

Inheritance diagram for CYITask< ResultType >:

Public Member Functions

 CYITask ()
 
 CYITask (CYIFuture< ResultType > &rFuture)
 
 CYITask (std::unique_ptr< CYIFuture< ResultType >> pFuture)
 
virtual ~CYITask ()
 
bool SetFuture (CYIFuture< ResultType > &rFuture)
 
bool SetFuture (std::unique_ptr< CYIFuture< ResultType >> rFuture)
 
- Public Member Functions inherited from CYITaskBase
virtual ~CYITaskBase ()
 
virtual bool TakeOwnershipOfFuture (std::unique_ptr< CYIAbstractFuture > pFuture)
 
TASK_STATE GetState () const
 
bool IsFutureOwner () const
 
bool MarkPendingExecution ()
 
void RequestCancellation ()
 
bool Execute ()
 

Protected Member Functions

virtual ResultType Run ()=0
 
- Protected Member Functions inherited from CYITaskBase
bool IsCancellationRequested () const
 
void SetCancellationRequestSucceeded ()
 
virtual void CleanUpAfterCancellation ()
 

Additional Inherited Members

- Public Types inherited from CYITaskBase
enum  TASK_STATE {
  TASK_NEW,
  TASK_PENDING_EXECUTION,
  TASK_EXECUTING,
  TASK_COMPLETED,
  TASK_CANCELLED
}
 
- Protected Attributes inherited from CYITaskBase
CYIMutex m_mutex
 

Constructor & Destructor Documentation

template<typename ResultType = void>
CYITask< ResultType >::CYITask ( )
template<typename ResultType = void>
CYITask< ResultType >::CYITask ( CYIFuture< ResultType > &  rFuture)
template<typename ResultType = void>
CYITask< ResultType >::CYITask ( std::unique_ptr< CYIFuture< ResultType >>  pFuture)
template<typename ResultType = void>
virtual CYITask< ResultType >::~CYITask ( )
virtual

Member Function Documentation

template<typename ResultType = void>
virtual ResultType CYITask< ResultType >::Run ( )
protectedpure virtual

The function executed by this task. Implement this function in a sub-classe to implement your own task.

Returns
Returns the result of the task's execution.

Implemented in CYIStaticTask< ResultType >.

template<typename ResultType = void>
bool CYITask< ResultType >::SetFuture ( CYIFuture< ResultType > &  rFuture)

Creates an association between the provided Future object and this Task. If a Future object is already associated with this Task, this function does nothing. If the provided Future object already has an associated task, this function does nothing.

Note
Associatations with Future objects can only be done prior to execution of the task and prior to enqueing the task in a task executor.
Returns
Returns true if the provided Future object was successfully associated with this Task.
template<typename ResultType = void>
bool CYITask< ResultType >::SetFuture ( std::unique_ptr< CYIFuture< ResultType >>  rFuture)

Creates an association between the provided Future object and this Task, and transfers ownership of the future object to this task. If a Future object is already associated with this Task, the provided future object is deleted. If the provided Future object already has an associated task, the provided future object is deleted.

Note
Associatations with Future objects can only be done prior to execution of the task and prior to enqueing the task in a task executor.
Returns
Returns true if the provided Future object was successfully associated with this Task.

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