Logger System 0.1.3
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 122 of file network_writer.cpp.

Member Typedef Documentation

◆ reconnect_callback

Definition at line 124 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 126 of file network_writer.cpp.

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

◆ ~network_reconnect_jthread_worker()

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

Definition at line 132 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 136 of file network_writer.cpp.

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

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 181 of file network_writer.cpp.

181 {
182 if (!running_.exchange(false, std::memory_order_acq_rel)) {
183 return; // Already stopped
184 }
185
186 // Request stop and join thread
188 thread_.join();
189 }
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 192 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 193 of file network_writer.cpp.

Referenced by start().

◆ running_

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

Definition at line 195 of file network_writer.cpp.

195{false};

Referenced by start(), and stop().

◆ thread_

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

Definition at line 194 of file network_writer.cpp.

Referenced by start(), and stop().


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