Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
thread_base.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
18#pragma once
19
25 // end of core_threading
26
29#include "thread_conditions.h"
30#include "error_handling.h"
31#include "thread_impl.h"
33
34#include <mutex>
35#include <memory>
36#include <thread>
37#include <atomic>
38#include <chrono>
39#include <iostream>
40#include <optional>
41#include <condition_variable>
42
43#ifdef USE_STD_JTHREAD
44#include <stop_token>
45#endif
46
65namespace kcenon::thread
66{
142 {
143 public:
144 thread_base(const thread_base&) = delete;
148
157 thread_base(const std::string& thread_title = "thread_base");
158
165 virtual ~thread_base(void);
166
177 auto set_wake_interval(const std::optional<std::chrono::milliseconds>& wake_interval)
178 -> void;
179
186 [[nodiscard]] auto get_wake_interval() const
187 -> std::optional<std::chrono::milliseconds>;
188
198 auto start(void) -> common::VoidResult;
199
209 auto stop(void) -> common::VoidResult;
210
215 [[nodiscard]] auto get_thread_title() const -> std::string { return thread_title_; }
216
224 [[nodiscard]] auto is_running() const -> bool;
225
235 [[nodiscard]] auto get_thread_id() const -> std::thread::id;
236
244 [[nodiscard]] virtual auto to_string(void) const -> std::string;
245
246 protected:
255 [[nodiscard]] virtual auto should_continue_work(void) const -> bool { return false; }
256
264 virtual auto before_start(void) -> common::VoidResult { return common::ok(); }
265
274 virtual auto do_work(void) -> common::VoidResult { return common::ok(); }
275
283 virtual auto after_stop(void) -> common::VoidResult { return common::ok(); }
284
302 virtual auto on_stop_requested(void) -> void {}
303
304 protected:
313 std::optional<std::chrono::milliseconds> wake_interval_;
314
315 private:
323 std::atomic<int> consecutive_failures_{0};
324
331 static constexpr int max_consecutive_failures = 10;
332
339 mutable std::mutex wake_interval_mutex_;
340
348
349#ifdef USE_STD_JTHREAD
355 std::unique_ptr<std::jthread> worker_thread_;
356#else
362 std::unique_ptr<std::thread> worker_thread_;
363#endif
364
368 std::string thread_title_;
369 };
370} // namespace kcenon::thread
371
372// ----------------------------------------------------------------------------
373// Formatter specializations for thread_base
374// ----------------------------------------------------------------------------
375
377
378// Generate formatter specializations for kcenon::thread::thread_base
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.
Generic formatter for enum types using user-provided converter functors.
Provides macros for generating std::formatter specializations.
Centralized thread lifecycle state and synchronization management.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
STL namespace.
Enumeration of thread lifecycle states (starting, running, stopping, stopped).
#define DECLARE_FORMATTER(CLASS_NAME)
Generates std::formatter specializations for narrow and wide characters.