41#include <condition_variable>
157 thread_base(
const std::string& thread_title =
"thread_base");
177 auto set_wake_interval(
const std::optional<std::chrono::milliseconds>& wake_interval)
187 ->
std::optional<
std::chrono::milliseconds>;
198 auto
start(
void) -> common::VoidResult;
209 auto
stop(
void) -> common::VoidResult;
224 [[nodiscard]]
auto is_running() const ->
bool;
244 [[nodiscard]] virtual auto
to_string(
void) const ->
std::
string;
264 virtual auto before_start(
void) -> common::VoidResult {
return common::ok(); }
274 virtual auto do_work(
void) -> common::VoidResult {
return common::ok(); }
283 virtual auto after_stop(
void) -> common::VoidResult {
return common::ok(); }
349#ifdef USE_STD_JTHREAD
Centralized thread lifecycle state and synchronization management.
A foundational class for implementing custom worker threads.
thread_base & operator=(thread_base &&)=delete
thread_base(thread_base &&)=delete
virtual ~thread_base(void)
Virtual destructor. Ensures proper cleanup of derived classes.
virtual auto to_string(void) const -> std::string
Returns a string representation of this thread_base object.
virtual auto do_work(void) -> common::VoidResult
The main work routine for the worker thread.
auto get_thread_title() const -> std::string
Returns the worker thread's title.
thread_base & operator=(const thread_base &)=delete
static constexpr int max_consecutive_failures
Maximum number of consecutive failures before stopping the thread.
std::mutex wake_interval_mutex_
Mutex for synchronizing access to the wake_interval_ member.
virtual auto after_stop(void) -> common::VoidResult
Called immediately after the worker thread has stopped.
auto stop(void) -> common::VoidResult
Requests the worker thread to stop and waits for it to finish.
thread_base(const thread_base &)=delete
auto get_thread_id() const -> std::thread::id
Gets the native thread ID of the worker thread.
std::unique_ptr< std::thread > worker_thread_
A std::thread for managing the worker thread's lifecycle (legacy mode).
auto is_running() const -> bool
Checks whether the worker thread is currently running.
std::optional< std::chrono::milliseconds > wake_interval_
Interval at which the thread is optionally awakened.
auto set_wake_interval(const std::optional< std::chrono::milliseconds > &wake_interval) -> void
Sets the interval at which the worker thread should wake up (if any).
std::atomic< int > consecutive_failures_
Counter for consecutive failures in do_work() execution.
lifecycle_controller lifecycle_
Lifecycle controller managing thread state and synchronization.
auto get_wake_interval() const -> std::optional< std::chrono::milliseconds >
Gets the current wake interval setting.
std::string thread_title_
A string title for identifying or naming the worker thread.
virtual auto on_stop_requested(void) -> void
Called when stop() is requested, before the thread actually stops.
virtual auto before_start(void) -> common::VoidResult
Called just before the worker thread starts running.
virtual auto should_continue_work(void) const -> bool
Determines whether the thread should continue doing work.
auto start(void) -> common::VoidResult
Starts the worker thread.
Error codes and utilities for the thread system.
Thread implementation abstraction bridging std::jthread and std::thread.
String encoding conversion, Base64 encoding/decoding utilities.
Centralized thread lifecycle state and synchronization management.
Core threading foundation of the thread system library.
Enumeration of thread lifecycle states (starting, running, stopping, stopped).