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

Wrapper for std::jthread or std::thread with manual stop mechanism. More...

#include <jthread_compat.h>

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

Public Member Functions

 compat_jthread () noexcept=default
 Default constructor - no thread.
 
template<typename F >
 compat_jthread (F &&func)
 Construct and start thread with stop token support.
 
 ~compat_jthread ()
 Destructor - requests stop and joins.
 
 compat_jthread (const compat_jthread &)=delete
 
compat_jthreadoperator= (const compat_jthread &)=delete
 
 compat_jthread (compat_jthread &&other) noexcept
 
compat_jthreadoperator= (compat_jthread &&other) noexcept
 
bool joinable () const noexcept
 Check if thread is joinable.
 
void request_stop ()
 Request the thread to stop.
 
void join ()
 Wait for thread to complete.
 
std::shared_ptr< simple_stop_sourceget_stop_source () const noexcept
 Get the stop source (fallback mode only)
 

Private Attributes

std::shared_ptr< simple_stop_sourcestop_source_
 
std::thread thread_
 

Detailed Description

Wrapper for std::jthread or std::thread with manual stop mechanism.

When KCENON_HAS_JTHREAD is true, this is a thin wrapper around std::jthread. When KCENON_HAS_JTHREAD is false, this provides equivalent functionality using std::thread with a simple_stop_source.

Definition at line 84 of file jthread_compat.h.

Constructor & Destructor Documentation

◆ compat_jthread() [1/4]

kcenon::logger::async::compat_jthread::compat_jthread ( )
defaultnoexcept

Default constructor - no thread.

◆ compat_jthread() [2/4]

template<typename F >
kcenon::logger::async::compat_jthread::compat_jthread ( F && func)
inlineexplicit

Construct and start thread with stop token support.

Template Parameters
FCallable type
Parameters
funcFunction to run in thread

The function signature should be:

  • For jthread: void(std::stop_token)
  • For fallback: void(simple_stop_source&)

Definition at line 106 of file jthread_compat.h.

107 : stop_source_(std::make_shared<simple_stop_source>())
108 , thread_([func = std::forward<F>(func), stop = stop_source_]() {
109 func(*stop);
110 }) {}
std::shared_ptr< simple_stop_source > stop_source_

◆ ~compat_jthread()

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

Destructor - requests stop and joins.

Definition at line 116 of file jthread_compat.h.

116 {
117 if (joinable()) {
118 request_stop();
119 join();
120 }
121 }
bool joinable() const noexcept
Check if thread is joinable.
void request_stop()
Request the thread to stop.
void join()
Wait for thread to complete.

References join(), joinable(), and request_stop().

Here is the call graph for this function:

◆ compat_jthread() [3/4]

kcenon::logger::async::compat_jthread::compat_jthread ( const compat_jthread & )
delete

◆ compat_jthread() [4/4]

kcenon::logger::async::compat_jthread::compat_jthread ( compat_jthread && other)
inlinenoexcept

Definition at line 128 of file jthread_compat.h.

130 : thread_(std::move(other.thread_)) {}
131#else
132 : stop_source_(std::move(other.stop_source_))
133 , thread_(std::move(other.thread_)) {}

Member Function Documentation

◆ get_stop_source()

std::shared_ptr< simple_stop_source > kcenon::logger::async::compat_jthread::get_stop_source ( ) const
inlinenodiscardnoexcept

Get the stop source (fallback mode only)

Returns
Shared pointer to the stop source

This is used by async_worker to pass the stop source to the worker loop, ensuring the same stop source is used for both request_stop() and stop_requested() checks.

Definition at line 200 of file jthread_compat.h.

200 {
201 return stop_source_;
202 }

References stop_source_.

◆ join()

void kcenon::logger::async::compat_jthread::join ( )
inline

Wait for thread to complete.

Definition at line 179 of file jthread_compat.h.

179 {
180#if KCENON_HAS_JTHREAD
181 if (thread_.joinable()) {
182 thread_.join();
183 }
184#else
185 if (thread_.joinable()) {
186 thread_.join();
187 }
188#endif
189 }

References thread_.

Referenced by operator=(), kcenon::logger::async::async_worker::stop(), kcenon::logger::async::batch_processing_jthread_worker::stop(), kcenon::logger::log_collector_jthread_worker::stop(), kcenon::logger::network_reconnect_jthread_worker::stop(), kcenon::logger::network_send_jthread_worker::stop(), and ~compat_jthread().

Here is the caller graph for this function:

◆ joinable()

bool kcenon::logger::async::compat_jthread::joinable ( ) const
inlinenodiscardnoexcept

Check if thread is joinable.

Definition at line 155 of file jthread_compat.h.

155 {
156#if KCENON_HAS_JTHREAD
157 return thread_.joinable();
158#else
159 return thread_.joinable();
160#endif
161 }

References thread_.

Referenced by operator=(), and ~compat_jthread().

Here is the caller graph for this function:

◆ operator=() [1/2]

compat_jthread & kcenon::logger::async::compat_jthread::operator= ( compat_jthread && other)
inlinenoexcept

Definition at line 136 of file jthread_compat.h.

136 {
137 if (this != &other) {
138 if (joinable()) {
139 request_stop();
140 join();
141 }
142#if KCENON_HAS_JTHREAD
143 thread_ = std::move(other.thread_);
144#else
145 stop_source_ = std::move(other.stop_source_);
146 thread_ = std::move(other.thread_);
147#endif
148 }
149 return *this;
150 }

References join(), joinable(), request_stop(), stop_source_, and thread_.

Here is the call graph for this function:

◆ operator=() [2/2]

compat_jthread & kcenon::logger::async::compat_jthread::operator= ( const compat_jthread & )
delete

◆ request_stop()

void kcenon::logger::async::compat_jthread::request_stop ( )
inline

Request the thread to stop.

Definition at line 166 of file jthread_compat.h.

166 {
167#if KCENON_HAS_JTHREAD
168 thread_.request_stop();
169#else
170 if (stop_source_) {
171 stop_source_->request_stop();
172 }
173#endif
174 }

References stop_source_, and thread_.

Referenced by operator=(), kcenon::logger::async::async_worker::stop(), kcenon::logger::async::batch_processing_jthread_worker::stop(), kcenon::logger::log_collector_jthread_worker::stop(), kcenon::logger::network_reconnect_jthread_worker::stop(), kcenon::logger::network_send_jthread_worker::stop(), and ~compat_jthread().

Here is the caller graph for this function:

Member Data Documentation

◆ stop_source_

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

Definition at line 209 of file jthread_compat.h.

Referenced by get_stop_source(), operator=(), and request_stop().

◆ thread_

std::thread kcenon::logger::async::compat_jthread::thread_
private

Definition at line 210 of file jthread_compat.h.

Referenced by join(), joinable(), operator=(), and request_stop().


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