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

Worker thread for reconnection attempts with jthread compatibility. More...

Collaboration diagram for kcenon::logger::network_reconnect_jthread_worker:
Collaboration graph

Public Types

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

Public Member Functions

 network_reconnect_jthread_worker (reconnect_callback callback, std::chrono::seconds interval)
 
 ~network_reconnect_jthread_worker ()
 
void start ()
 
void stop ()
 

Private Attributes

reconnect_callback callback_
 
std::chrono::seconds reconnect_interval_
 
compat_jthread thread_
 
std::atomic< bool > running_ {false}
 

Detailed Description

Worker thread for reconnection attempts 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 123 of file network_writer.cpp.

Member Typedef Documentation

◆ reconnect_callback

Definition at line 125 of file network_writer.cpp.

Constructor & Destructor Documentation

◆ network_reconnect_jthread_worker()

kcenon::logger::network_reconnect_jthread_worker::network_reconnect_jthread_worker ( reconnect_callback callback,
std::chrono::seconds interval )
inlineexplicit

Definition at line 127 of file network_writer.cpp.

129 : callback_(std::move(callback))
130 , reconnect_interval_(interval)
131 {}

◆ ~network_reconnect_jthread_worker()

kcenon::logger::network_reconnect_jthread_worker::~network_reconnect_jthread_worker ( )
inline

Definition at line 133 of file network_writer.cpp.

References stop().

Here is the call graph for this function:

Member Function Documentation

◆ start()

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

Definition at line 137 of file network_writer.cpp.

137 {
138 if (running_.exchange(true, std::memory_order_acq_rel)) {
139 return; // Already started
140 }
141
142#if LOGGER_HAS_JTHREAD
143 auto callback = callback_;
144 auto interval = reconnect_interval_;
145 thread_ = compat_jthread([callback, interval](std::stop_token stop_token) {
146 while (!stop_token.stop_requested()) {
147 if (callback) {
148 callback();
149 }
150 // Sleep with stop checking
151 for (auto elapsed = std::chrono::milliseconds{0};
152 elapsed < std::chrono::duration_cast<std::chrono::milliseconds>(interval)
153 && !stop_token.stop_requested();
154 elapsed += std::chrono::milliseconds(100)) {
155 std::this_thread::sleep_for(std::chrono::milliseconds(100));
156 }
157 }
158 });
159#else
160 // Create worker thread with manual stop source
161 // Note: We use the stop_source created by compat_jthread to ensure
162 // request_stop() correctly signals the worker loop
163 auto callback = callback_;
164 auto interval = reconnect_interval_;
165 thread_ = compat_jthread([callback, interval](simple_stop_source& stop) {
166 while (!stop.stop_requested()) {
167 if (callback) {
168 callback();
169 }
170 // Sleep with stop checking
171 for (auto elapsed = std::chrono::milliseconds{0};
172 elapsed < std::chrono::duration_cast<std::chrono::milliseconds>(interval)
173 && !stop.stop_requested();
174 elapsed += std::chrono::milliseconds(100)) {
175 std::this_thread::sleep_for(std::chrono::milliseconds(100));
176 }
177 }
178 });
179#endif
180 }

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

Here is the call graph for this function:

◆ stop()

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

Definition at line 182 of file network_writer.cpp.

182 {
183 if (!running_.exchange(false, std::memory_order_acq_rel)) {
184 return; // Already stopped
185 }
186
187 // Request stop and join thread
189 thread_.join();
190 }
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_reconnect_jthread_worker().

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

Member Data Documentation

◆ callback_

reconnect_callback kcenon::logger::network_reconnect_jthread_worker::callback_
private

Definition at line 193 of file network_writer.cpp.

Referenced by start().

◆ reconnect_interval_

std::chrono::seconds kcenon::logger::network_reconnect_jthread_worker::reconnect_interval_
private

Definition at line 194 of file network_writer.cpp.

Referenced by start().

◆ running_

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

Definition at line 196 of file network_writer.cpp.

196{false};

Referenced by start(), and stop().

◆ thread_

compat_jthread kcenon::logger::network_reconnect_jthread_worker::thread_
private

Definition at line 195 of file network_writer.cpp.

Referenced by start(), and stop().


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