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

Worker thread for sending buffered logs with jthread compatibility. More...

Collaboration diagram for kcenon::logger::network_send_jthread_worker:
Collaboration graph

Public Types

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

Public Member Functions

 network_send_jthread_worker (send_callback callback)
 
 ~network_send_jthread_worker ()
 
void start ()
 
void stop ()
 
void notify_work ()
 

Private Attributes

send_callback callback_
 
compat_jthread thread_
 
std::atomic< bool > running_ {false}
 
std::atomic< bool > has_work_ {false}
 

Detailed Description

Worker thread for sending buffered logs with jthread compatibility.

Uses std::jthread with std::stop_token for cooperative cancellation where available, falls back to std::thread with manual stop mechanism.

Definition at line 53 of file network_writer.cpp.

Member Typedef Documentation

◆ send_callback

Definition at line 55 of file network_writer.cpp.

Constructor & Destructor Documentation

◆ network_send_jthread_worker()

kcenon::logger::network_send_jthread_worker::network_send_jthread_worker ( send_callback callback)
inlineexplicit

Definition at line 57 of file network_writer.cpp.

58 : callback_(std::move(callback))
59 {}

◆ ~network_send_jthread_worker()

kcenon::logger::network_send_jthread_worker::~network_send_jthread_worker ( )
inline

Definition at line 61 of file network_writer.cpp.

References stop().

Here is the call graph for this function:

Member Function Documentation

◆ notify_work()

void kcenon::logger::network_send_jthread_worker::notify_work ( )
inline

Definition at line 106 of file network_writer.cpp.

106 {
107 has_work_.store(true, std::memory_order_release);
108 }

References has_work_.

◆ start()

void kcenon::logger::network_send_jthread_worker::start ( )
inline

Definition at line 65 of file network_writer.cpp.

65 {
66 if (running_.exchange(true, std::memory_order_acq_rel)) {
67 return; // Already started
68 }
69
70#if LOGGER_HAS_JTHREAD
71 auto callback = callback_;
72 thread_ = compat_jthread([callback](std::stop_token stop_token) {
73 while (!stop_token.stop_requested()) {
74 if (callback) {
75 callback();
76 }
77 std::this_thread::sleep_for(std::chrono::milliseconds(10));
78 }
79 });
80#else
81 // Create worker thread with manual stop source
82 // Note: We use the stop_source created by compat_jthread to ensure
83 // request_stop() correctly signals the worker loop
84 auto callback = callback_;
85 thread_ = compat_jthread([callback](simple_stop_source& stop) {
86 while (!stop.stop_requested()) {
87 if (callback) {
88 callback();
89 }
90 std::this_thread::sleep_for(std::chrono::milliseconds(10));
91 }
92 });
93#endif
94 }

References callback_, running_, stop(), and thread_.

Here is the call graph for this function:

◆ stop()

void kcenon::logger::network_send_jthread_worker::stop ( )
inline

Definition at line 96 of file network_writer.cpp.

96 {
97 if (!running_.exchange(false, std::memory_order_acq_rel)) {
98 return; // Already stopped
99 }
100
101 // Request stop and join thread
103 thread_.join();
104 }
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 ~network_send_jthread_worker().

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

Member Data Documentation

◆ callback_

send_callback kcenon::logger::network_send_jthread_worker::callback_
private

Definition at line 111 of file network_writer.cpp.

Referenced by start().

◆ has_work_

std::atomic<bool> kcenon::logger::network_send_jthread_worker::has_work_ {false}
private

Definition at line 114 of file network_writer.cpp.

114{false};

Referenced by notify_work().

◆ running_

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

Definition at line 113 of file network_writer.cpp.

113{false};

Referenced by start(), and stop().

◆ thread_

compat_jthread kcenon::logger::network_send_jthread_worker::thread_
private

Definition at line 112 of file network_writer.cpp.

Referenced by start(), and stop().


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