Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
kcenon::logger::async::batch_processing_jthread_worker Class Reference

Worker thread for batch processing with jthread compatibility. More...

Collaboration diagram for kcenon::logger::async::batch_processing_jthread_worker:
Collaboration graph

Public Types

using process_callback = std::function<void()>
 

Public Member Functions

 batch_processing_jthread_worker (process_callback callback, std::mutex &notify_mutex, std::condition_variable &notify_cv)
 
 ~batch_processing_jthread_worker ()
 
void start ()
 
void stop ()
 
bool is_running () const noexcept
 

Private Attributes

process_callback callback_
 
std::mutex & notify_mutex_
 
std::condition_variable & notify_cv_
 
compat_jthread thread_
 
std::atomic< bool > running_ {false}
 
std::shared_ptr< simple_stop_sourcestop_source_
 

Detailed Description

Worker thread for batch processing with jthread compatibility.

Uses std::jthread with std::stop_token for cooperative cancellation where available, falls back to std::thread with manual stop mechanism for environments without jthread support (e.g., libc++).

Definition at line 33 of file batch_processor.cpp.

Member Typedef Documentation

◆ process_callback

Definition at line 35 of file batch_processor.cpp.

Constructor & Destructor Documentation

◆ batch_processing_jthread_worker()

kcenon::logger::async::batch_processing_jthread_worker::batch_processing_jthread_worker ( process_callback callback,
std::mutex & notify_mutex,
std::condition_variable & notify_cv )
inlineexplicit

Definition at line 37 of file batch_processor.cpp.

40 : callback_(std::move(callback))
41 , notify_mutex_(notify_mutex)
42 , notify_cv_(notify_cv)
43#if !LOGGER_HAS_JTHREAD
44 , stop_source_(std::make_shared<simple_stop_source>())
45#endif
46 {}
std::shared_ptr< simple_stop_source > stop_source_

◆ ~batch_processing_jthread_worker()

kcenon::logger::async::batch_processing_jthread_worker::~batch_processing_jthread_worker ( )
inline

Definition at line 48 of file batch_processor.cpp.

References stop().

Here is the call graph for this function:

Member Function Documentation

◆ is_running()

bool kcenon::logger::async::batch_processing_jthread_worker::is_running ( ) const
inlinenodiscardnoexcept

Definition at line 104 of file batch_processor.cpp.

104 {
105 return running_.load(std::memory_order_acquire);
106 }

References running_.

◆ start()

void kcenon::logger::async::batch_processing_jthread_worker::start ( )
inline

Definition at line 52 of file batch_processor.cpp.

52 {
53 if (running_.exchange(true, std::memory_order_acq_rel)) {
54 return; // Already started
55 }
56
57#if LOGGER_HAS_JTHREAD
58 auto callback = callback_;
59 auto& cv = notify_cv_;
60 auto& mtx = notify_mutex_;
61 thread_ = compat_jthread([callback, &cv, &mtx](std::stop_token stop_token) {
62 while (!stop_token.stop_requested()) {
63 if (callback) {
64 callback();
65 }
66 // Wait for notification or timeout instead of polling
67 std::unique_lock<std::mutex> lock(mtx);
68 cv.wait_for(lock, std::chrono::milliseconds(10),
69 [&stop_token]{ return stop_token.stop_requested(); });
70 }
71 });
72#else
73 // Reset stop source for new start
74 stop_source_->reset();
75
76 auto callback = callback_;
77 auto stop = stop_source_;
78 auto& cv = notify_cv_;
79 auto& mtx = notify_mutex_;
80 thread_ = compat_jthread([callback, stop, &cv, &mtx](simple_stop_source& /*unused*/) {
81 while (!stop->stop_requested()) {
82 if (callback) {
83 callback();
84 }
85 // Wait for notification or timeout instead of polling
86 std::unique_lock<std::mutex> lock(mtx);
87 cv.wait_for(lock, std::chrono::milliseconds(10),
88 [&stop]{ return stop->stop_requested(); });
89 }
90 });
91#endif
92 }

References callback_, notify_cv_, notify_mutex_, running_, stop(), stop_source_, and thread_.

Here is the call graph for this function:

◆ stop()

void kcenon::logger::async::batch_processing_jthread_worker::stop ( )
inline

Definition at line 94 of file batch_processor.cpp.

94 {
95 if (!running_.exchange(false, std::memory_order_acq_rel)) {
96 return; // Already stopped
97 }
98
99 // Request stop and join thread
101 thread_.join();
102 }
void request_stop()
Request the thread to stop.
void join()
Wait for thread to complete.

References kcenon::logger::async::compat_jthread::join(), kcenon::logger::async::compat_jthread::request_stop(), running_, and thread_.

Referenced by start(), and ~batch_processing_jthread_worker().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ callback_

process_callback kcenon::logger::async::batch_processing_jthread_worker::callback_
private

Definition at line 109 of file batch_processor.cpp.

Referenced by start().

◆ notify_cv_

std::condition_variable& kcenon::logger::async::batch_processing_jthread_worker::notify_cv_
private

Definition at line 111 of file batch_processor.cpp.

Referenced by start().

◆ notify_mutex_

std::mutex& kcenon::logger::async::batch_processing_jthread_worker::notify_mutex_
private

Definition at line 110 of file batch_processor.cpp.

Referenced by start().

◆ running_

std::atomic<bool> kcenon::logger::async::batch_processing_jthread_worker::running_ {false}
private

Definition at line 113 of file batch_processor.cpp.

113{false};

Referenced by is_running(), start(), and stop().

◆ stop_source_

std::shared_ptr<simple_stop_source> kcenon::logger::async::batch_processing_jthread_worker::stop_source_
private

Definition at line 115 of file batch_processor.cpp.

Referenced by start().

◆ thread_

compat_jthread kcenon::logger::async::batch_processing_jthread_worker::thread_
private

Definition at line 112 of file batch_processor.cpp.

Referenced by start(), and stop().


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