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.
All public functions, with the exception of the destructor, are thread-safe.
| ResultType | the type of the execution result of this task. |
#include <thread/YiTask.h>

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 |
| CYITask< ResultType >::CYITask | ( | CYIFuture< ResultType > & | rFuture | ) |
| CYITask< ResultType >::CYITask | ( | std::unique_ptr< CYIFuture< ResultType >> | pFuture | ) |
|
protectedpure virtual |
The function executed by this task. Implement this function in a sub-classe to implement your own task.
Implemented in CYIStaticTask< ResultType >.
| 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.
| 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.