LogIt++
Loading...
Searching...
No Matches
TaskExecutor.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _LOGIT_TASK_EXECUTOR_HPP_INCLUDED
3#define _LOGIT_TASK_EXECUTOR_HPP_INCLUDED
6
7#include <thread>
8#include <queue>
9#include <mutex>
10#include <functional>
11#include <condition_variable>
12
13namespace logit {
14
21 public:
25 static TaskExecutor* instance = new TaskExecutor();
26 return *instance;
27 }
28
31 void add_task(std::function<void()> task) {
32 std::unique_lock<std::mutex> lock(m_queue_mutex);
33 if (m_stop_flag) return;
34 m_tasks_queue.push(std::move(task));
35 lock.unlock();
36 m_queue_condition.notify_one();
37 }
38
40 void wait() {
41 m_queue_condition.notify_one();
42 for (;;) {
43 std::unique_lock<std::mutex> lock(m_queue_mutex);
44 if (m_tasks_queue.empty() || m_stop_flag) break;
45 lock.unlock();
46 std::this_thread::yield();
47 std::this_thread::sleep_for(std::chrono::milliseconds(1));
48 }
49 }
50
53 void shutdown() {
54 std::unique_lock<std::mutex> lock(m_queue_mutex);
55 m_stop_flag = true;
56 lock.unlock();
57 m_queue_condition.notify_one();
58 if (m_worker_thread.joinable()) {
59 m_worker_thread.join();
60 }
61 }
62
63 private:
64 std::queue<std::function<void()>> m_tasks_queue;
65 mutable std::mutex m_queue_mutex;
66 std::condition_variable m_queue_condition;
67 std::thread m_worker_thread;
69
72 for (;;) {
73 std::function<void()> task;
74 std::unique_lock<std::mutex> lock(m_queue_mutex);
75 m_queue_condition.wait(lock, [this]() {
76 return !m_tasks_queue.empty() || m_stop_flag;
77 });
78 if (m_stop_flag && m_tasks_queue.empty()) {
79 break;
80 }
81 task = std::move(m_tasks_queue.front());
82 m_tasks_queue.pop();
83 lock.unlock();
84 task();
85 }
86 }
87
91 }
92
95 shutdown();
96 }
97
98 // Delete copy constructor and assignment operators to enforce singleton usage.
99 TaskExecutor(const TaskExecutor&) = delete;
103 };
104
105}; // namespace logit
106
107#endif // _LOGIT_TASK_EXECUTOR_HPP_INCLUDED
void add_task(std::function< void()> task)
Adds a task to the queue in a thread-safe manner.
std::mutex m_queue_mutex
Mutex to protect access to the task queue.
~TaskExecutor()
Destructor that stops the worker thread and cleans up resources.
void shutdown()
Shuts down the TaskExecutor by stopping the worker thread.
TaskExecutor(TaskExecutor &&)=delete
bool m_stop_flag
Flag indicating if the worker thread should stop.
TaskExecutor(const TaskExecutor &)=delete
std::queue< std::function< void()> > m_tasks_queue
Queue holding tasks to be executed.
TaskExecutor & operator=(TaskExecutor &&)=delete
void wait()
Waits for all tasks in the queue to be processed.
TaskExecutor & operator=(const TaskExecutor &)=delete
std::thread m_worker_thread
Worker thread for executing tasks.
void worker_function()
The worker thread function that processes tasks from the queue.
static TaskExecutor & get_instance()
Get the singleton instance of the TaskExecutor.
std::condition_variable m_queue_condition
Condition variable to signal task availability.
TaskExecutor()
Private constructor to enforce the singleton pattern.
The primary namespace for the LogIt++ library.