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 copy of 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. Deleting the task will result in the future being marked as cancelled, but deleting the future will not result in the task being cancelled.
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, in continuations being started, 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 > future) | |
bool | SetFuture (CYIFuture< ResultType > future) |
![]() | |
virtual | ~CYITaskBase () |
State | GetState () const |
bool | MarkPendingExecution () |
void | RequestCancellation () |
bool | IsCancellationRequested () const |
void | SetCancellationRequestSucceeded () |
bool | IsCancellationRequestSucceeded () const |
bool | Execute () |
Protected Member Functions | |
virtual ResultType | Run ()=0 |
Additional Inherited Members | |
![]() | |
enum | State : uint8_t { State::New, State::PendingExecution, State::Executing, State::Completed, State::Cancelled } |
|
explicit |
|
protectedpure virtual |
The function executed by this task. Implement this function in a sub-classe to implement your own task.
Implemented in CYIStaticTask< ResultType >, and CYICallableTask< ResultType, Callable, Args >.
bool CYITask< ResultType >::SetFuture | ( | CYIFuture< ResultType > | future | ) |
Creates an association between the provided CYIFuture object and this CYITask. If a CYIFuture object is already associated with this CYITask, this function does nothing and false is returned. If the provided CYIFuture object already has an associated task, this function does nothing and false is returned.