|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
Centralized thread lifecycle state and synchronization management. More...
#include <lifecycle_controller.h>

Public Member Functions | |
| lifecycle_controller (const lifecycle_controller &)=delete | |
| lifecycle_controller & | operator= (const lifecycle_controller &)=delete |
| lifecycle_controller (lifecycle_controller &&)=delete | |
| lifecycle_controller & | operator= (lifecycle_controller &&)=delete |
| lifecycle_controller () | |
| Constructs a new lifecycle_controller in Created state. | |
| ~lifecycle_controller ()=default | |
| Destructor. | |
| auto | get_state () const noexcept -> thread_conditions |
| Gets the current thread condition/state. | |
| auto | set_state (thread_conditions state) noexcept -> void |
| Sets the thread condition/state. | |
| auto | is_running () const noexcept -> bool |
| Checks if the thread is currently running. | |
| auto | set_stopped () noexcept -> void |
| Marks the thread as stopped. | |
| auto | initialize_for_start () -> void |
| Initializes the controller for a new thread start. | |
| auto | request_stop () noexcept -> void |
| Requests the thread to stop. | |
| auto | is_stop_requested () const noexcept -> bool |
| Checks if a stop has been requested. | |
| auto | has_active_source () const noexcept -> bool |
| Checks if the controller has an active stop source (C++20 only). | |
| auto | reset_stop_source () noexcept -> void |
| Resets the stop control mechanism after thread completion. | |
| auto | acquire_lock () -> std::unique_lock< std::mutex > |
| Acquires a unique lock on the condition variable mutex. | |
| template<typename Predicate > | |
| auto | wait (std::unique_lock< std::mutex > &lock, Predicate pred) -> void |
| Waits on the condition variable with a predicate. | |
| template<typename Rep , typename Period , typename Predicate > | |
| auto | wait_for (std::unique_lock< std::mutex > &lock, const std::chrono::duration< Rep, Period > &timeout, Predicate pred) -> bool |
| Waits on the condition variable with a timeout and predicate. | |
| auto | notify_one () -> void |
| Notifies one waiting thread. | |
| auto | notify_all () -> void |
| Notifies all waiting threads. | |
Private Attributes | |
| std::mutex | cv_mutex_ |
| Mutex for condition variable operations. | |
| std::condition_variable | condition_ |
| Condition variable for thread signaling. | |
| std::atomic< thread_conditions > | state_ {thread_conditions::Created} |
| Current thread state. | |
| std::atomic< bool > | stop_requested_ {false} |
| Atomic flag for stop request (legacy mode). | |
Centralized thread lifecycle state and synchronization management.
The lifecycle_controller class consolidates duplicated thread lifecycle management patterns (start, stop, state transitions, condition variables) into a single reusable component. Thread classes can use composition with this controller instead of implementing these patterns themselves.
All public methods are thread-safe. The class uses internal synchronization to protect state transitions and condition variable operations.
Definition at line 79 of file lifecycle_controller.h.
|
delete |
|
delete |
| kcenon::thread::lifecycle_controller::lifecycle_controller | ( | ) |
Constructs a new lifecycle_controller in Created state.
Definition at line 9 of file lifecycle_controller.cpp.
|
default |
Destructor.
|
nodiscard |
Acquires a unique lock on the condition variable mutex.
Use this to prepare for wait operations.
Definition at line 95 of file lifecycle_controller.cpp.
|
nodiscardnoexcept |
Gets the current thread condition/state.
Thread Safety:
Definition at line 19 of file lifecycle_controller.cpp.
References state_.
|
nodiscardnoexcept |
Checks if the controller has an active stop source (C++20 only).
In legacy mode, checks if stop has NOT been requested (indicating active state).
Definition at line 76 of file lifecycle_controller.cpp.
References state_, kcenon::thread::Waiting, and kcenon::thread::Working.
| auto kcenon::thread::lifecycle_controller::initialize_for_start | ( | ) | -> void |
Initializes the controller for a new thread start.
Resets the stop request flag and prepares for a new thread lifecycle. In C++20 jthread mode, creates a new stop_source.
Definition at line 41 of file lifecycle_controller.cpp.
References kcenon::thread::Created.
|
nodiscardnoexcept |
Checks if the thread is currently running.
Definition at line 29 of file lifecycle_controller.cpp.
References state_, kcenon::thread::Waiting, and kcenon::thread::Working.
|
nodiscardnoexcept |
Checks if a stop has been requested.
Thread Safety:
Definition at line 63 of file lifecycle_controller.cpp.
References stop_requested_.
| auto kcenon::thread::lifecycle_controller::notify_all | ( | ) | -> void |
Notifies all waiting threads.
Thread Safety:
Definition at line 106 of file lifecycle_controller.cpp.
| auto kcenon::thread::lifecycle_controller::notify_one | ( | ) | -> void |
Notifies one waiting thread.
Thread Safety:
Definition at line 100 of file lifecycle_controller.cpp.
|
delete |
|
delete |
|
noexcept |
Requests the thread to stop.
In C++20 mode, calls request_stop() on the stop_source. In legacy mode, sets the atomic stop_requested_ flag to true.
Thread Safety:
Definition at line 51 of file lifecycle_controller.cpp.
References stop_requested_.
|
noexcept |
Resets the stop control mechanism after thread completion.
In C++20 mode, resets the stop_source. Should be called after thread join to clean up resources.
Definition at line 88 of file lifecycle_controller.cpp.
|
noexcept |
Sets the thread condition/state.
| state | The new thread_conditions value. |
Thread Safety:
Definition at line 24 of file lifecycle_controller.cpp.
Referenced by set_stopped().

|
noexcept |
Marks the thread as stopped.
Convenience method equivalent to set_state(thread_conditions::Stopped).
Definition at line 36 of file lifecycle_controller.cpp.
References set_state(), and kcenon::thread::Stopped.

|
inline |
Waits on the condition variable with a predicate.
| Predicate | A callable returning bool. |
| lock | The unique_lock (must be holding cv_mutex_). |
| pred | The predicate to check. |
Waits until pred() returns true OR stop is requested.
Definition at line 206 of file lifecycle_controller.h.
|
inline |
Waits on the condition variable with a timeout and predicate.
| Rep | Duration rep type. |
| Period | Duration period type. |
| Predicate | A callable returning bool. |
| lock | The unique_lock (must be holding cv_mutex_). |
| timeout | The maximum duration to wait. |
| pred | The predicate to check. |
Definition at line 238 of file lifecycle_controller.h.
|
private |
Condition variable for thread signaling.
Definition at line 282 of file lifecycle_controller.h.
|
private |
Mutex for condition variable operations.
Definition at line 279 of file lifecycle_controller.h.
|
private |
Current thread state.
Definition at line 285 of file lifecycle_controller.h.
Referenced by get_state(), has_active_source(), and is_running().
|
private |
Atomic flag for stop request (legacy mode).
Definition at line 292 of file lifecycle_controller.h.
Referenced by is_stop_requested(), and request_stop().