This class handles concurrent execution of tasks. More...

#include <TaskRunner.h>

Inherits TaskQueue.

Public Member Functions

 TaskRunner ()
 Constructor. More...
 
 TaskRunner (unsigned int threadCount)
 Constructor. More...
 
virtual ~TaskRunner ()=default
 
 TaskRunner (const TaskRunner &other)=delete
 
TaskRunneroperator= (const TaskRunner &other)=delete
 
void AddTask (std::unique_ptr< Task > task)
 Adds a task to the task queue. This should only be called from one thread. More...
 
void Run ()
 Runs all tasks and waits for completion. More...
 
virtual std::unique_ptr< TaskGetNextTask () override
 Removes and returns the next task from the task queue. If the queue is empty, a null pointer is returned. More...
 
void EchoProgress (bool echo)
 
virtual void ReportTaskComplete () override
 Notification that a task is complete. More...
 

Protected Member Functions

void PrintProgress ()
 Prints a text progress bar based on the number of tasks there were in the queue when execution began and the number of reported task completions. More...
 

Protected Attributes

std::recursive_mutex m_taskQueueMutex
 Mutex used to synchronize concurrent access to the task queue. More...
 
std::queue< std::unique_ptr< Task > > m_tasks
 Queue of tasks to be run. More...
 
unsigned int m_threadCount = 1
 The number of threads to spawn when running tasks. More...
 
bool m_echoPercentComplete = false
 Whether or not the task runner should output percent complete as it executes. More...
 
size_t m_totalTaskCount = 0
 Total number of tasks in the queue when the task running begins to execute. More...
 
unsigned int m_tasksComplete = 0
 Number of completed tasks. More...
 

Detailed Description

This class handles concurrent execution of tasks.

Constructor & Destructor Documentation

◆ TaskRunner() [1/3]

TaskRunner::TaskRunner ( )

Constructor.

The default constructor sets the thread count to the number of cores on the machine as determined by std::thread::hardware_concurrency(). In the case that the number of cores cannot be determined, the TaskRunner runs in single-threaded mode.

◆ TaskRunner() [2/3]

TaskRunner::TaskRunner ( unsigned int  threadCount)

Constructor.

Parameters
threadCountnumber of threads to use

◆ ~TaskRunner()

virtual TaskRunner::~TaskRunner ( )
virtualdefault

◆ TaskRunner() [3/3]

TaskRunner::TaskRunner ( const TaskRunner other)
delete

Member Function Documentation

◆ AddTask()

void TaskRunner::AddTask ( std::unique_ptr< Task task)

Adds a task to the task queue. This should only be called from one thread.

Parameters
taskUnique pointer to the task to add

◆ EchoProgress()

void TaskRunner::EchoProgress ( bool  echo)
inline

◆ GetNextTask()

std::unique_ptr< Task > TaskRunner::GetNextTask ( )
overridevirtual

Removes and returns the next task from the task queue. If the queue is empty, a null pointer is returned.

Returns
Pointer to the next task, or a null pointer if there are no tasks

Implements TaskQueue.

◆ operator=()

TaskRunner& TaskRunner::operator= ( const TaskRunner other)
delete

◆ PrintProgress()

void TaskRunner::PrintProgress ( )
protected

Prints a text progress bar based on the number of tasks there were in the queue when execution began and the number of reported task completions.

◆ ReportTaskComplete()

void TaskRunner::ReportTaskComplete ( )
overridevirtual

Notification that a task is complete.

Implements TaskQueue.

◆ Run()

void TaskRunner::Run ( )

Runs all tasks and waits for completion.

Member Data Documentation

◆ m_echoPercentComplete

bool TaskRunner::m_echoPercentComplete = false
protected

Whether or not the task runner should output percent complete as it executes.

◆ m_taskQueueMutex

std::recursive_mutex TaskRunner::m_taskQueueMutex
mutableprotected

Mutex used to synchronize concurrent access to the task queue.

◆ m_tasks

std::queue<std::unique_ptr<Task> > TaskRunner::m_tasks
protected

Queue of tasks to be run.

◆ m_tasksComplete

unsigned int TaskRunner::m_tasksComplete = 0
protected

Number of completed tasks.

◆ m_threadCount

unsigned int TaskRunner::m_threadCount = 1
protected

The number of threads to spawn when running tasks.

◆ m_totalTaskCount

size_t TaskRunner::m_totalTaskCount = 0
protected

Total number of tasks in the queue when the task running begins to execute.