|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
A foundational class for implementing custom worker threads. More...
#include <thread_base.h>


Public Member Functions | |
| thread_base (const thread_base &)=delete | |
| thread_base & | operator= (const thread_base &)=delete |
| thread_base (thread_base &&)=delete | |
| thread_base & | operator= (thread_base &&)=delete |
| thread_base (const std::string &thread_title="thread_base") | |
Constructs a new thread_base object. | |
| virtual | ~thread_base (void) |
| Virtual destructor. Ensures proper cleanup of derived classes. | |
| 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). | |
| auto | get_wake_interval () const -> std::optional< std::chrono::milliseconds > |
| Gets the current wake interval setting. | |
| auto | start (void) -> common::VoidResult |
| Starts the worker thread. | |
| auto | stop (void) -> common::VoidResult |
| Requests the worker thread to stop and waits for it to finish. | |
| auto | get_thread_title () const -> std::string |
| Returns the worker thread's title. | |
| auto | is_running () const -> bool |
| Checks whether the worker thread is currently running. | |
| auto | get_thread_id () const -> std::thread::id |
| Gets the native thread ID of the worker thread. | |
| virtual auto | to_string (void) const -> std::string |
Returns a string representation of this thread_base object. | |
Protected Member Functions | |
| virtual auto | should_continue_work (void) const -> bool |
| Determines whether the thread should continue doing work. | |
| virtual auto | before_start (void) -> common::VoidResult |
| Called just before the worker thread starts running. | |
| virtual auto | do_work (void) -> common::VoidResult |
| The main work routine for the worker thread. | |
| virtual auto | after_stop (void) -> common::VoidResult |
| Called immediately after the worker thread has stopped. | |
| virtual auto | on_stop_requested (void) -> void |
| Called when stop() is requested, before the thread actually stops. | |
Protected Attributes | |
| std::optional< std::chrono::milliseconds > | wake_interval_ |
| Interval at which the thread is optionally awakened. | |
Private Attributes | |
| std::atomic< int > | consecutive_failures_ {0} |
| Counter for consecutive failures in do_work() execution. | |
| std::mutex | wake_interval_mutex_ |
| Mutex for synchronizing access to the wake_interval_ member. | |
| lifecycle_controller | lifecycle_ |
| Lifecycle controller managing thread state and synchronization. | |
| std::unique_ptr< std::thread > | worker_thread_ |
A std::thread for managing the worker thread's lifecycle (legacy mode). | |
| std::string | thread_title_ |
| A string title for identifying or naming the worker thread. | |
Static Private Attributes | |
| static constexpr int | max_consecutive_failures = 10 |
| Maximum number of consecutive failures before stopping the thread. | |
A foundational class for implementing custom worker threads.
The thread_base class provides a framework for managing a single worker thread, offering lifecycle methods (start, stop), optional wake intervals, and hooks (before_start, do_work, after_stop) for derived classes to customize behavior.
This class abstracts platform-specific thread management details and provides a unified interface for both C++20 std::jthread and traditional std::thread, selected at compile time via the USE_STD_JTHREAD macro.
std::jthread or custom mechanism)All public methods in thread_base are thread-safe. The class uses internal synchronization mechanisms to protect its state.
thread_base and override do_work(), before_start(), and/or after_stop() as needed.start() to launch the thread.stop().should_continue_work() or internal conditions to determine if it should continue running.Definition at line 141 of file thread_base.h.
|
delete |
|
delete |
| kcenon::thread::thread_base::thread_base | ( | const std::string & | thread_title = "thread_base" | ) |
Constructs a new thread_base object.
Constructs a new thread_base instance with the specified title.
| thread_title | A human-readable title for this worker thread (default: "thread_base"). |
The thread_title can be useful for logging, debugging, or thread naming (where the platform supports it).
Implementation details:
Definition at line 27 of file thread_base.cpp.
|
virtual |
Virtual destructor. Ensures proper cleanup of derived classes.
Destroys the thread_base instance, stopping the thread if needed.
If the thread is still running when the destructor is called, stop() is typically called internally to join the thread before destruction.
Implementation details:
Definition at line 46 of file thread_base.cpp.
References stop().

|
inlineprotectedvirtual |
Called immediately after the worker thread has stopped.
common::VoidResult containing an error on failure, or success value if successful.Override this method in derived classes to perform any cleanup or finalization tasks once the worker thread has fully exited.
Definition at line 283 of file thread_base.h.
|
inlineprotectedvirtual |
Called just before the worker thread starts running.
common::VoidResult containing an error on failure, or success value if successful.Override this method in derived classes to perform any initialization or setup required before the worker thread begins its main loop.
Definition at line 264 of file thread_base.h.
|
inlineprotectedvirtual |
The main work routine for the worker thread.
common::VoidResult containing an error on failure, or success value if successful.Derived classes should override this method to implement the actual work the thread needs to perform. This method is called repeatedly (in an internal loop) until the thread is signaled to stop or should_continue_work() returns false.
Reimplemented in kcenon::thread::thread_worker, kcenon::thread::typed_thread_worker_t< job_type >, and kcenon::thread::typed_thread_worker_t< job_types >.
Definition at line 274 of file thread_base.h.
|
nodiscard |
Gets the native thread ID of the worker thread.
Thread Safety:
Implementation details:
Definition at line 355 of file thread_base.cpp.
References worker_thread_.
|
inlinenodiscard |
Returns the worker thread's title.
Definition at line 215 of file thread_base.h.
References thread_title_.
|
nodiscard |
Gets the current wake interval setting.
std::nullopt if periodic wake-ups are disabled.Implementation details:
Thread Safety:
Definition at line 83 of file thread_base.cpp.
References wake_interval_, and wake_interval_mutex_.
|
nodiscard |
Checks whether the worker thread is currently running.
Checks if the worker thread is currently active.
true if the thread is running, false otherwise.This depends on the thread's internal condition state (e.g., thread_conditions::running).
Implementation details:
Definition at line 324 of file thread_base.cpp.
|
inlineprotectedvirtual |
Called when stop() is requested, before the thread actually stops.
This hook allows derived classes to perform cancellation-related operations, such as signaling running jobs to cancel. This is called from the thread requesting the stop (not from the worker thread itself).
The default implementation does nothing. Override this to propagate cancellation signals to any active work items.
Thread Safety:
Reimplemented in kcenon::thread::thread_worker.
Definition at line 302 of file thread_base.h.
|
delete |
|
delete |
| auto kcenon::thread::thread_base::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).
Sets the wake interval for periodic thread wake-ups.
| wake_interval | Duration in milliseconds for periodic wake-ups, or std::nullopt to disable periodic wake-ups. |
If a wake interval is set, the worker thread can periodically perform some action (e.g., housekeeping tasks) even if there's no immediate external signal.
Implementation details:
Thread Safety:
Definition at line 61 of file thread_base.cpp.
|
inlinenodiscardprotectedvirtual |
Determines whether the thread should continue doing work.
true if there is work to do, false otherwise.The default implementation always returns false (indicating no ongoing work). Override this in derived classes if you wish the thread to perform repeated tasks until some condition changes.
Reimplemented in kcenon::thread::thread_worker, kcenon::thread::typed_thread_worker_t< job_type >, and kcenon::thread::typed_thread_worker_t< job_types >.
Definition at line 255 of file thread_base.h.
| auto kcenon::thread::thread_base::start | ( | void | ) | -> common::VoidResult |
Starts the worker thread.
Starts the worker thread and begins execution loop.
common::VoidResult containing an error on failure, or success value if successful.Internally, this method:
before_start() to allow derived classes to perform setup.std::jthread or std::thread).do_work() until the thread is signaled to stop.Implementation details:
Main Work Loop Logic:
Definition at line 110 of file thread_base.cpp.
References kcenon::thread::critical, kcenon::thread::error, kcenon::thread::thread_logger::instance(), kcenon::thread::thread_logger::log(), kcenon::thread::resource_allocation_failed, kcenon::thread::Stopping, kcenon::thread::thread_already_running, kcenon::thread::Waiting, and kcenon::thread::Working.

| auto kcenon::thread::thread_base::stop | ( | void | ) | -> common::VoidResult |
Requests the worker thread to stop and waits for it to finish.
Stops the worker thread and waits for it to complete.
common::VoidResult containing an error on failure, or success value if successful.Internally, this method:
stop_source_ or stop_requested_).after_stop() for post-shutdown cleanup in derived classes.Implementation details:
Shutdown Sequence:
Definition at line 271 of file thread_base.cpp.
References kcenon::thread::invalid_argument, and kcenon::thread::thread_not_running.
Referenced by ~thread_base().

|
nodiscardvirtual |
Returns a string representation of this thread_base object.
Provides a string representation of the thread's current state.
Derived classes may override this to include additional details (e.g., current status, counters, or other state).
Implementation details:
Definition at line 340 of file thread_base.cpp.
References utility_module::formatter::format().
Referenced by std::formatter< kcenon::thread::thread_worker >::format(), std::formatter< kcenon::thread::thread_worker, wchar_t >::format(), std::formatter< kcenon::thread::typed_thread_worker_t< job_type > >::format(), and std::formatter< kcenon::thread::typed_thread_worker_t< job_type >, wchar_t >::format().


|
private |
Counter for consecutive failures in do_work() execution.
This counter is incremented each time do_work() throws an exception. It is reset to 0 when do_work() completes successfully. Used to implement exponential backoff and prevent infinite error loops.
Definition at line 323 of file thread_base.h.
|
private |
Lifecycle controller managing thread state and synchronization.
Consolidates condition variable, mutex, stop request, and state management into a single reusable component.
Definition at line 347 of file thread_base.h.
|
staticconstexprprivate |
Maximum number of consecutive failures before stopping the thread.
If do_work() throws exceptions this many times in a row, the thread will stop automatically to prevent infinite error loops and resource exhaustion.
Definition at line 331 of file thread_base.h.
|
private |
A string title for identifying or naming the worker thread.
Definition at line 368 of file thread_base.h.
Referenced by get_thread_title().
|
protected |
Interval at which the thread is optionally awakened.
If set, the worker thread can wake periodically (in addition to any other wake conditions) to perform tasks at regular intervals.
Definition at line 313 of file thread_base.h.
Referenced by get_wake_interval().
|
mutableprivate |
Mutex for synchronizing access to the wake_interval_ member.
This mutex must be acquired when reading or writing wake_interval_ to prevent data races between the setter and the worker thread.
Definition at line 339 of file thread_base.h.
Referenced by get_wake_interval().
|
private |
A std::thread for managing the worker thread's lifecycle (legacy mode).
If USE_STD_JTHREAD is not defined, we fall back to a standard thread.
Definition at line 362 of file thread_base.h.
Referenced by get_thread_id().