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 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.

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.
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

#include <thread/YiTask.h>

Inheritance diagram for CYITask< ResultType >:

Public Member Functions

 CYITask ()
 CYITask (CYIFuture< ResultType > future)
bool SetFuture (CYIFuture< ResultType > future)
- Public Member Functions inherited from CYITaskBase
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

- Public Types inherited from CYITaskBase
enum  State : uint8_t {

Constructor & Destructor Documentation

◆ CYITask() [1/2]

template<typename ResultType = void>
CYITask< ResultType >::CYITask ( )

◆ CYITask() [2/2]

template<typename ResultType = void>
CYITask< ResultType >::CYITask ( CYIFuture< ResultType >  future)

Member Function Documentation

◆ Run()

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 the result of the task's execution.

Implemented in CYIStaticTask< ResultType >, and CYICallableTask< ResultType, Callable, Args >.

◆ SetFuture()

template<typename ResultType = void>
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.

Associatations with CYIFuture objects can only be done prior to execution of the task and prior to enqueing the task in a task executor.
This function is not threadsafe.
Returns true if the provided CYIFuture object was successfully associated with this Task.

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