20#include <condition_variable>
125 [[nodiscard]] auto
is_running() const noexcept ->
bool;
205 template<typename Predicate>
206 auto
wait(
std::unique_lock<
std::mutex>& lock, Predicate pred) ->
void
208#ifdef USE_STD_JTHREAD
209 if (stop_source_.has_value())
211 auto stop_token = stop_source_.value().get_token();
212 condition_.wait(lock, [this, &stop_token, &pred]() {
213 return stop_token.stop_requested() || pred();
237 template<
typename Rep,
typename Period,
typename Predicate>
239 const std::chrono::duration<Rep, Period>& timeout,
240 Predicate pred) ->
bool
242#ifdef USE_STD_JTHREAD
243 if (stop_source_.has_value())
245 auto stop_token = stop_source_.value().get_token();
246 return condition_.wait_for(lock, timeout, [
this, &stop_token, &pred]() {
247 return stop_token.stop_requested() || pred();
252 return condition_.wait_for(lock, timeout, pred);
255 return condition_.wait_for(lock, timeout, [
this, &pred]() {
256 return stop_requested_.load(std::memory_order_acquire) || pred();
267 auto notify_one() -> void;
275 auto notify_all() -> void;
287#ifdef USE_STD_JTHREAD
289 std::optional<std::stop_source> stop_source_;
292 std::atomic<bool> stop_requested_{
false};
Centralized thread lifecycle state and synchronization management.
auto wait(std::unique_lock< std::mutex > &lock, Predicate pred) -> void
Waits on the condition variable with a predicate.
auto is_stop_requested() const noexcept -> bool
Checks if a stop has been requested.
auto set_state(thread_conditions state) noexcept -> void
Sets the thread condition/state.
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 request_stop() noexcept -> void
Requests the thread to stop.
auto has_active_source() const noexcept -> bool
Checks if the controller has an active stop source (C++20 only).
lifecycle_controller(lifecycle_controller &&)=delete
auto initialize_for_start() -> void
Initializes the controller for a new thread start.
~lifecycle_controller()=default
Destructor.
auto set_stopped() noexcept -> void
Marks the thread as stopped.
std::atomic< bool > stop_requested_
Atomic flag for stop request (legacy mode).
auto acquire_lock() -> std::unique_lock< std::mutex >
Acquires a unique lock on the condition variable mutex.
auto is_running() const noexcept -> bool
Checks if the thread is currently running.
lifecycle_controller()
Constructs a new lifecycle_controller in Created state.
auto reset_stop_source() noexcept -> void
Resets the stop control mechanism after thread completion.
std::mutex cv_mutex_
Mutex for condition variable operations.
lifecycle_controller & operator=(const lifecycle_controller &)=delete
auto get_state() const noexcept -> thread_conditions
Gets the current thread condition/state.
lifecycle_controller & operator=(lifecycle_controller &&)=delete
std::condition_variable condition_
Condition variable for thread signaling.
lifecycle_controller(const lifecycle_controller &)=delete
Core threading foundation of the thread system library.
thread_conditions
Enumeration of various states in a thread's lifecycle.
@ Created
Thread created but not started.
Enumeration of thread lifecycle states (starting, running, stopping, stopped).