28 : wake_interval_(
std::nullopt)
30 , worker_thread_(nullptr)
31 , thread_title_(thread_title)
62 const std::optional<std::chrono::milliseconds>& wake_interval) ->
void
65 std::scoped_lock<std::mutex> lock(wake_interval_mutex_);
66 wake_interval_ = wake_interval;
84 ->
std::optional<
std::chrono::milliseconds>
113 if (lifecycle_.has_active_source())
122 lifecycle_.initialize_for_start();
127#ifdef USE_STD_JTHREAD
128 worker_thread_ = std::make_unique<std::jthread>(
130 worker_thread_ = std::make_unique<std::thread>(
135 auto work_result = before_start();
136 if (work_result.is_err())
138 std::cerr <<
"error before start: " << work_result.error().message
143 while (!lifecycle_.is_stop_requested() || should_continue_work())
149 auto interval = get_wake_interval();
152 auto lock = lifecycle_.acquire_lock();
155 if (interval.has_value())
158 lifecycle_.wait_for(lock, interval.value(),
159 [
this]() { return should_continue_work(); });
164 lifecycle_.wait(lock,
165 [
this]() {
return should_continue_work(); });
169 if (lifecycle_.is_stop_requested() && !should_continue_work())
183 work_result = do_work();
184 if (work_result.is_err())
190 "Work execution failed",
191 work_result.error().message);
195 consecutive_failures_.store(0, std::memory_order_relaxed);
197 catch (
const std::exception& e)
200 int failures = consecutive_failures_.fetch_add(1, std::memory_order_relaxed) + 1;
205 "Unhandled exception in worker thread (failure " + std::to_string(failures) +
")",
209 if (failures >= max_consecutive_failures)
214 "Too many consecutive failures, stopping thread",
221 auto backoff_ms = std::min(100 * (1 << std::min(failures - 1, 10)), 10000);
222 std::this_thread::sleep_for(std::chrono::milliseconds(backoff_ms));
227 work_result = after_stop();
228 if (work_result.is_err())
233 "Error during cleanup",
234 work_result.error().message);
238 catch (
const std::bad_alloc& e)
241 lifecycle_.reset_stop_source();
242 worker_thread_.reset();
274 if (worker_thread_ ==
nullptr)
280 if (worker_thread_->joinable())
284 if (worker_thread_->get_id() == std::this_thread::get_id())
287 "cannot stop thread from within itself - would cause deadlock",
"thread_system"};
291 lifecycle_.request_stop();
298 lifecycle_.notify_all();
301 worker_thread_->join();
305 lifecycle_.reset_stop_source();
306 worker_thread_.reset();
309 lifecycle_.set_stopped();
326 return lifecycle_.is_running();
361 return std::thread::id{};
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.
std::mutex wake_interval_mutex_
Mutex for synchronizing access to the wake_interval_ member.
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).
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.
static thread_logger & instance()
Get singleton instance.
void log(log_level level, std::string_view thread_name, std::string_view message, std::string_view context="")
Log a message with context.
Core threading foundation of the thread system library.
@ resource_allocation_failed
@ Waiting
Thread waiting for work or tasks.
@ Stopping
Thread in the process of stopping.
@ Working
Thread currently processing a task.
Foundational worker thread class with lifecycle management.
Internal logging interface for the thread system.