LogIt++
Loading...
Searching...
No Matches
logit::TaskExecutor Class Reference

A thread-safe task executor that processes tasks in a dedicated worker thread. More...

#include <TaskExecutor.hpp>

Public Member Functions

void add_task (std::function< void()> task)
 Adds a task to the queue in a thread-safe manner.
 
void wait ()
 Waits for all tasks in the queue to be processed.
 
void shutdown ()
 Shuts down the TaskExecutor by stopping the worker thread.
 

Static Public Member Functions

static TaskExecutorget_instance ()
 Get the singleton instance of the TaskExecutor.
 

Private Member Functions

void worker_function ()
 The worker thread function that processes tasks from the queue.
 
 TaskExecutor ()
 Private constructor to enforce the singleton pattern.
 
 ~TaskExecutor ()
 Destructor that stops the worker thread and cleans up resources.
 
 TaskExecutor (const TaskExecutor &)=delete
 
TaskExecutoroperator= (const TaskExecutor &)=delete
 
 TaskExecutor (TaskExecutor &&)=delete
 
TaskExecutoroperator= (TaskExecutor &&)=delete
 

Private Attributes

std::queue< std::function< void()> > m_tasks_queue
 Queue holding tasks to be executed.
 
std::mutex m_queue_mutex
 Mutex to protect access to the task queue.
 
std::condition_variable m_queue_condition
 Condition variable to signal task availability.
 
std::thread m_worker_thread
 Worker thread for executing tasks.
 
bool m_stop_flag
 Flag indicating if the worker thread should stop.
 

Detailed Description

A thread-safe task executor that processes tasks in a dedicated worker thread.

This class provides a mechanism for queuing tasks (functions or lambdas) and executing them asynchronously in a background thread. It follows the singleton design pattern.

Definition at line 20 of file TaskExecutor.hpp.

Constructor & Destructor Documentation

◆ TaskExecutor() [1/3]

logit::TaskExecutor::TaskExecutor ( )
inlineprivate

Private constructor to enforce the singleton pattern.

Definition at line 89 of file TaskExecutor.hpp.

◆ ~TaskExecutor()

logit::TaskExecutor::~TaskExecutor ( )
inlineprivate

Destructor that stops the worker thread and cleans up resources.

Definition at line 94 of file TaskExecutor.hpp.

◆ TaskExecutor() [2/3]

logit::TaskExecutor::TaskExecutor ( const TaskExecutor & )
privatedelete

◆ TaskExecutor() [3/3]

logit::TaskExecutor::TaskExecutor ( TaskExecutor && )
privatedelete

Member Function Documentation

◆ add_task()

void logit::TaskExecutor::add_task ( std::function< void()> task)
inline

Adds a task to the queue in a thread-safe manner.

Parameters
taskA function or lambda with no arguments to be executed asynchronously.

Definition at line 31 of file TaskExecutor.hpp.

◆ get_instance()

static TaskExecutor & logit::TaskExecutor::get_instance ( )
inlinestatic

Get the singleton instance of the TaskExecutor.

Returns
A reference to the single instance of TaskExecutor.

Definition at line 24 of file TaskExecutor.hpp.

◆ operator=() [1/2]

TaskExecutor & logit::TaskExecutor::operator= ( const TaskExecutor & )
privatedelete

◆ operator=() [2/2]

TaskExecutor & logit::TaskExecutor::operator= ( TaskExecutor && )
privatedelete

◆ shutdown()

void logit::TaskExecutor::shutdown ( )
inline

Shuts down the TaskExecutor by stopping the worker thread.

This method signals the worker thread to stop and then joins it.

Definition at line 53 of file TaskExecutor.hpp.

◆ wait()

void logit::TaskExecutor::wait ( )
inline

Waits for all tasks in the queue to be processed.

Definition at line 40 of file TaskExecutor.hpp.

◆ worker_function()

void logit::TaskExecutor::worker_function ( )
inlineprivate

The worker thread function that processes tasks from the queue.

Definition at line 71 of file TaskExecutor.hpp.

Member Data Documentation

◆ m_queue_condition

std::condition_variable logit::TaskExecutor::m_queue_condition
private

Condition variable to signal task availability.

Definition at line 66 of file TaskExecutor.hpp.

◆ m_queue_mutex

std::mutex logit::TaskExecutor::m_queue_mutex
mutableprivate

Mutex to protect access to the task queue.

Definition at line 65 of file TaskExecutor.hpp.

◆ m_stop_flag

bool logit::TaskExecutor::m_stop_flag
private

Flag indicating if the worker thread should stop.

Definition at line 68 of file TaskExecutor.hpp.

◆ m_tasks_queue

std::queue<std::function<void()> > logit::TaskExecutor::m_tasks_queue
private

Queue holding tasks to be executed.

Definition at line 64 of file TaskExecutor.hpp.

◆ m_worker_thread

std::thread logit::TaskExecutor::m_worker_thread
private

Worker thread for executing tasks.

Definition at line 67 of file TaskExecutor.hpp.


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