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

#include <TaskRunner.h>

Inheritance diagram for biogears::TaskRunner:

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

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

Private 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...
 
unsigned int 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

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

biogears::TaskRunner::TaskRunner ( unsigned int  threadCount)

Constructor.

Parameters
threadCountnumber of threads to use
virtual biogears::TaskRunner::~TaskRunner ( )
virtualdefault
biogears::TaskRunner::TaskRunner ( const TaskRunner other)
delete

Member Function Documentation

void biogears::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
void biogears::TaskRunner::EchoProgress ( bool  echo)
inline
std::unique_ptr< Task > biogears::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 biogears::TaskQueue.

TaskRunner& biogears::TaskRunner::operator= ( const TaskRunner other)
delete
void biogears::TaskRunner::PrintProgress ( )
private

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.

void biogears::TaskRunner::ReportTaskComplete ( )
overridevirtual

Notification that a task is complete.

Implements biogears::TaskQueue.

void biogears::TaskRunner::Run ( )

Runs all tasks and waits for completion.

Member Data Documentation

bool biogears::TaskRunner::m_echoPercentComplete = false
private

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

std::recursive_mutex biogears::TaskRunner::m_taskQueueMutex
mutableprivate

Mutex used to synchronize concurrent access to the task queue.

std::queue<std::unique_ptr<Task> > biogears::TaskRunner::m_tasks
private

Queue of tasks to be run.

unsigned int biogears::TaskRunner::m_tasksComplete = 0
private

Number of completed tasks.

unsigned int biogears::TaskRunner::m_threadCount = 1
private

The number of threads to spawn when running tasks.

unsigned int biogears::TaskRunner::m_totalTaskCount = 0
private

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