LogIt++
Loading...
Searching...
No Matches
logit::detail::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.
 
void set_max_queue_size (std::size_t size)
 Sets the maximum size of the task queue.
 
void set_queue_policy (QueuePolicy policy)
 Sets the behavior when the queue is full.
 

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.
 
std::size_t m_max_queue_size
 Maximum number of tasks in the queue (0 for unlimited).
 
QueuePolicy m_overflow_policy
 Policy for handling queue overflow.
 

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 58 of file TaskExecutor.hpp.

Constructor & Destructor Documentation

◆ TaskExecutor() [1/3]

logit::detail::TaskExecutor::TaskExecutor ( )
inlineprivate

Private constructor to enforce the singleton pattern.

Definition at line 153 of file TaskExecutor.hpp.

◆ ~TaskExecutor()

logit::detail::TaskExecutor::~TaskExecutor ( )
inlineprivate

Destructor that stops the worker thread and cleans up resources.

Definition at line 158 of file TaskExecutor.hpp.

◆ TaskExecutor() [2/3]

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

◆ TaskExecutor() [3/3]

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

Member Function Documentation

◆ add_task()

void logit::detail::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 69 of file TaskExecutor.hpp.

◆ get_instance()

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

Get the singleton instance of the TaskExecutor.

Returns
A reference to the single instance of TaskExecutor.

Definition at line 62 of file TaskExecutor.hpp.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ set_max_queue_size()

void logit::detail::TaskExecutor::set_max_queue_size ( std::size_t size)
inline

Sets the maximum size of the task queue.

Parameters
sizeMaximum number of tasks in the queue (0 for unlimited).

Definition at line 112 of file TaskExecutor.hpp.

◆ set_queue_policy()

void logit::detail::TaskExecutor::set_queue_policy ( QueuePolicy policy)
inline

Sets the behavior when the queue is full.

Parameters
policyQueuePolicy::Drop to discard tasks or QueuePolicy::Block to wait.

Definition at line 119 of file TaskExecutor.hpp.

◆ shutdown()

void logit::detail::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 100 of file TaskExecutor.hpp.

◆ wait()

void logit::detail::TaskExecutor::wait ( )
inline

Waits for all tasks in the queue to be processed.

Definition at line 87 of file TaskExecutor.hpp.

◆ worker_function()

void logit::detail::TaskExecutor::worker_function ( )
inlineprivate

The worker thread function that processes tasks from the queue.

Definition at line 134 of file TaskExecutor.hpp.

Member Data Documentation

◆ m_max_queue_size

std::size_t logit::detail::TaskExecutor::m_max_queue_size
private

Maximum number of tasks in the queue (0 for unlimited).

Definition at line 130 of file TaskExecutor.hpp.

◆ m_overflow_policy

QueuePolicy logit::detail::TaskExecutor::m_overflow_policy
private

Policy for handling queue overflow.

Definition at line 131 of file TaskExecutor.hpp.

◆ m_queue_condition

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

Condition variable to signal task availability.

Definition at line 127 of file TaskExecutor.hpp.

◆ m_queue_mutex

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

Mutex to protect access to the task queue.

Definition at line 126 of file TaskExecutor.hpp.

◆ m_stop_flag

bool logit::detail::TaskExecutor::m_stop_flag
private

Flag indicating if the worker thread should stop.

Definition at line 129 of file TaskExecutor.hpp.

◆ m_tasks_queue

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

Queue holding tasks to be executed.

Definition at line 125 of file TaskExecutor.hpp.

◆ m_worker_thread

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

Worker thread for executing tasks.

Definition at line 128 of file TaskExecutor.hpp.


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