![]() |
LogIt++
|
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 TaskExecutor & | get_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 | |
| TaskExecutor & | operator= (const TaskExecutor &)=delete |
| TaskExecutor (TaskExecutor &&)=delete | |
| TaskExecutor & | operator= (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. | |
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.
|
inlineprivate |
Private constructor to enforce the singleton pattern.
Definition at line 153 of file TaskExecutor.hpp.
|
inlineprivate |
Destructor that stops the worker thread and cleans up resources.
Definition at line 158 of file TaskExecutor.hpp.
|
privatedelete |
|
privatedelete |
|
inline |
Adds a task to the queue in a thread-safe manner.
| task | A function or lambda with no arguments to be executed asynchronously. |
Definition at line 69 of file TaskExecutor.hpp.
|
inlinestatic |
Get the singleton instance of the TaskExecutor.
TaskExecutor. Definition at line 62 of file TaskExecutor.hpp.
|
privatedelete |
|
privatedelete |
|
inline |
Sets the maximum size of the task queue.
| size | Maximum number of tasks in the queue (0 for unlimited). |
Definition at line 112 of file TaskExecutor.hpp.
|
inline |
Sets the behavior when the queue is full.
| policy | QueuePolicy::Drop to discard tasks or QueuePolicy::Block to wait. |
Definition at line 119 of file TaskExecutor.hpp.
|
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.
|
inline |
Waits for all tasks in the queue to be processed.
Definition at line 87 of file TaskExecutor.hpp.
|
inlineprivate |
The worker thread function that processes tasks from the queue.
Definition at line 134 of file TaskExecutor.hpp.
|
private |
Maximum number of tasks in the queue (0 for unlimited).
Definition at line 130 of file TaskExecutor.hpp.
|
private |
Policy for handling queue overflow.
Definition at line 131 of file TaskExecutor.hpp.
|
private |
Condition variable to signal task availability.
Definition at line 127 of file TaskExecutor.hpp.
|
mutableprivate |
Mutex to protect access to the task queue.
Definition at line 126 of file TaskExecutor.hpp.
|
private |
Flag indicating if the worker thread should stop.
Definition at line 129 of file TaskExecutor.hpp.
|
private |
Queue holding tasks to be executed.
Definition at line 125 of file TaskExecutor.hpp.
|
private |
Worker thread for executing tasks.
Definition at line 128 of file TaskExecutor.hpp.