GlitchyByte Lib  0.3.1
GlitchyByte general library to bootstrap development.
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
gb::Task Class Referenceabstract

Public Types

enum class  State : int { Created , Started , Canceled , Finished }
 

Public Member Functions

 Task () noexcept
 
virtual ~Task () noexcept=default
 
State getState () const noexcept
 
void cancel () noexcept
 
void awaitStop () noexcept
 
bool isStopped () const noexcept
 

Protected Member Functions

virtual void action () noexcept=0
 
TaskRunnergetTaskRunner () const noexcept
 
void started () noexcept
 
bool shouldCancel () const noexcept
 

Protected Attributes

std::mutex stateLock
 

Friends

class TaskRunner
 

Detailed Description

A single concurrent task that starts in a separate thread and runs until finished or the task is canceled.

Member Enumeration Documentation

◆ State

enum gb::Task::State : int
strong

Task states.

Enumerator
Created 
Started 
Canceled 
Finished 

Constructor & Destructor Documentation

◆ Task()

gb::Task::Task ( )
inlinenoexcept

Creates a task with a unique id.

◆ ~Task()

virtual gb::Task::~Task ( )
virtualdefaultnoexcept

Member Function Documentation

◆ getState()

State gb::Task::getState ( ) const
inlinenoexcept

Returns the current state of the task, as of the time of calling this method.

Note that the state can change by the time you are comparing the result.

Returns
The current state of the task.

◆ cancel()

void gb::Task::cancel ( )
inlinenoexcept

Signals the task to cancel.

It's up to the task itself to check for its cancellation and needing to exit.

◆ awaitStop()

void gb::Task::awaitStop ( )
inlinenoexcept

Blocks the calling thread until the task stops, either by cancellation or by finishing.

◆ isStopped()

bool gb::Task::isStopped ( ) const
inlinenoexcept

Tests if the task has stopped, either by cancellation or by finishing.

Once the task enters a stopped state its state will never change again.

Returns
True if the task has stopped.

◆ action()

virtual void gb::Task::action ( )
protectedpure virtualnoexcept

Actual action that is executed on its own thread.

Implementations MUST call started(), usually after initialization, indicating the task is ready and running.

◆ getTaskRunner()

TaskRunner* gb::Task::getTaskRunner ( ) const
inlineprotectednoexcept

Returns the task runner that is running this task.

Can be used to start other tasks with the same runner.

Returns
The task runner that is running this task.

◆ started()

void gb::Task::started ( )
inlineprotectednoexcept

Signals the tasks internals that the action has started and is functional.

It MUST be called within action by implementations.

◆ shouldCancel()

bool gb::Task::shouldCancel ( ) const
inlineprotectednoexcept

Tests if the task was canceled and should exit.

Long running action implementations should check this periodically and exit when requested.

Returns
True if the task was canceled and should exit.

Friends And Related Function Documentation

◆ TaskRunner

friend class TaskRunner
friend

Member Data Documentation

◆ stateLock

std::mutex gb::Task::stateLock
protected

Lock for any state related operation.


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