Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
async_worker.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
28#include "jthread_compat.h"
29
30#include <atomic>
31#include <chrono>
32#include <condition_variable>
33#include <functional>
34#include <memory>
35#include <mutex>
36#include <queue>
37#include <thread>
38
39namespace kcenon::logger::async {
40
47using task_type = std::function<void()>;
48
83public:
94 explicit async_worker(std::size_t queue_size = 8192);
95
104
105 // Non-copyable, non-movable (owns thread resource)
106 async_worker(const async_worker&) = delete;
110
120 void start();
121
137 void stop();
138
156 bool enqueue(task_type task);
157
169 void flush();
170
175 [[nodiscard]] bool is_running() const noexcept;
176
183 [[nodiscard]] std::size_t pending_count() const;
184
189 [[nodiscard]] std::size_t capacity() const noexcept;
190
195 [[nodiscard]] std::uint64_t dropped_count() const noexcept;
196
197private:
198#if LOGGER_HAS_JTHREAD
206 void worker_loop(std::stop_token stop_token);
207#else
216#endif
217
223 void drain_queue();
224
225private:
226 const std::size_t queue_size_;
227 std::queue<task_type> queue_;
228 mutable std::mutex queue_mutex_;
229
230#if LOGGER_HAS_JTHREAD
231 std::condition_variable_any queue_cv_;
232#else
233 std::condition_variable queue_cv_;
234#endif
235
237 std::atomic<bool> running_{false};
238 std::atomic<std::uint64_t> dropped_count_{0};
239};
240
241} // namespace kcenon::logger::async
Standalone async worker with jthread compatibility.
compat_jthread worker_thread_
Background worker thread.
const std::size_t queue_size_
Maximum queue capacity.
void worker_loop(simple_stop_source &stop)
Worker thread main loop (fallback version)
std::size_t pending_count() const
Get the current queue size.
void drain_queue()
Process all remaining tasks in the queue.
void start()
Start the worker thread.
~async_worker()
Destructor - ensures graceful shutdown.
async_worker & operator=(async_worker &&)=delete
async_worker(const async_worker &)=delete
void flush()
Flush all pending tasks.
std::atomic< bool > running_
Worker state flag.
async_worker & operator=(const async_worker &)=delete
void stop()
Stop the worker thread gracefully.
std::queue< task_type > queue_
Task queue.
std::atomic< std::uint64_t > dropped_count_
Overflow counter.
std::size_t capacity() const noexcept
Get the queue capacity.
std::mutex queue_mutex_
Protects queue_ access.
async_worker(async_worker &&)=delete
std::uint64_t dropped_count() const noexcept
Get the number of dropped tasks due to queue overflow.
std::condition_variable queue_cv_
Signals new tasks or stop (fallback)
bool is_running() const noexcept
Check if the worker is currently running.
bool enqueue(task_type task)
Enqueue a task for async processing.
async_worker(std::size_t queue_size=8192)
Constructor with configurable queue size.
Wrapper for std::jthread or std::thread with manual stop mechanism.
Simple stop source for environments without std::stop_token.
Compatibility header for std::jthread and std::stop_token kcenon.
std::function< void()> task_type
Task type for async worker.