|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
A thread pool for concurrent execution of jobs using multiple worker threads. More...
#include <thread_pool.h>


Public Member Functions | |
| thread_pool (const std::string &thread_title="thread_pool", const thread_context &context=thread_context()) | |
Constructs a new thread_pool instance. | |
| thread_pool (const std::string &thread_title, std::shared_ptr< job_queue > custom_queue, const thread_context &context=thread_context()) | |
Constructs a new thread_pool instance with a custom job queue. | |
| thread_pool (const std::string &thread_title, std::unique_ptr< pool_queue_adapter_interface > queue_adapter, const thread_context &context=thread_context()) | |
Constructs a new thread_pool instance with a policy_queue adapter. | |
| virtual | ~thread_pool (void) |
| Virtual destructor. Cleans up resources used by the thread pool. | |
| auto | get_ptr (void) -> std::shared_ptr< thread_pool > |
Retrieves a std::shared_ptr to this thread_pool instance. | |
| auto | start (void) -> common::VoidResult |
| Starts the thread pool and all associated workers. | |
| auto | get_job_queue (void) -> std::shared_ptr< job_queue > |
Returns the shared job_queue used by this thread pool. | |
| const metrics::ThreadPoolMetrics & | metrics () const noexcept |
| Access aggregated runtime metrics (read-only reference). | |
| void | reset_metrics () |
| Reset accumulated metrics. | |
| void | set_enhanced_metrics_enabled (bool enabled) |
| Enable or disable enhanced metrics collection. | |
| bool | is_enhanced_metrics_enabled () const |
| Check if enhanced metrics is enabled. | |
| const metrics::EnhancedThreadPoolMetrics & | enhanced_metrics () const |
| Access enhanced metrics (read-only reference). | |
| metrics::EnhancedSnapshot | enhanced_metrics_snapshot () const |
| Get enhanced metrics snapshot. | |
| auto | enqueue (std::unique_ptr< job > &&job) -> common::VoidResult |
Enqueues a new job into the shared job_queue. | |
| auto | enqueue_batch (std::vector< std::unique_ptr< job > > &&jobs) -> common::VoidResult |
Enqueues a batch of jobs into the shared job_queue. | |
| auto | enqueue (std::unique_ptr< thread_worker > &&worker) -> common::VoidResult |
Adds a thread_worker to the thread pool for specialized or additional processing. | |
| auto | enqueue_batch (std::vector< std::unique_ptr< thread_worker > > &&workers) -> common::VoidResult |
Adds a batch of thread_worker objects to the thread pool. | |
| auto | stop (const bool &immediately_stop=false) -> common::VoidResult |
| Stops the thread pool and all worker threads. | |
| auto | to_string (void) const -> std::string |
Provides a string representation of this thread_pool. | |
| std::uint32_t | get_pool_instance_id () const |
| Get the pool instance id. | |
| void | report_metrics () |
| Collect and report current thread pool metrics. | |
| std::size_t | get_idle_worker_count () const |
| Get the number of idle workers. | |
| auto | get_context (void) const -> const thread_context & |
| Gets the thread context for this pool. | |
| template<typename F , typename R = std::invoke_result_t<std::decay_t<F>>> | |
| auto | submit (F &&callable, const submit_options &opts={}) -> std::future< R > |
| template<typename F , typename R = std::invoke_result_t<std::decay_t<F>>> | |
| auto | submit (std::vector< F > &&callables, const submit_options &opts={}) -> std::vector< std::future< R > > |
| template<typename F , typename R = std::invoke_result_t<std::decay_t<F>>> | |
| auto | submit_wait_all (std::vector< F > &&callables, const submit_options &opts={}) -> std::vector< R > |
| Submit a batch and wait for all results. | |
| template<typename F , typename R = std::invoke_result_t<std::decay_t<F>>> | |
| auto | submit_wait_any (std::vector< F > &&callables, const submit_options &opts={}) -> common::Result< R > |
| Submit a batch and return first completed result. | |
| auto | is_running () const -> bool |
| Check if the thread pool is currently running. | |
| auto | get_pending_task_count () const -> std::size_t |
| Get the number of pending tasks in the queue. | |
| auto | check_worker_health (bool restart_failed=true) -> std::size_t |
| Check health of all worker threads and restart failed workers. | |
| auto | get_active_worker_count () const -> std::size_t |
| Get the current number of active (running) workers. | |
| void | add_policy (std::unique_ptr< pool_policy > policy) |
| Add a policy to the pool. | |
| auto | get_policies () const -> const std::vector< std::unique_ptr< pool_policy > > & |
| Get all registered policies. | |
| template<typename T = pool_policy> | |
| auto | find_policy (const std::string &name) -> T * |
| Find a policy by name. | |
| auto | remove_policy (const std::string &name) -> bool |
| Remove a policy by name. | |
| auto | diagnostics () -> diagnostics::thread_pool_diagnostics & |
| Get the diagnostics interface for this pool. | |
| auto | diagnostics () const -> const diagnostics::thread_pool_diagnostics & |
| Get the diagnostics interface for this pool (const version). | |
| auto | collect_worker_diagnostics () const -> std::vector< diagnostics::thread_info > |
| Collects diagnostics information from all workers. | |
Private Member Functions | |
| auto | stop_unsafe () noexcept -> common::VoidResult |
| Stops the thread pool without logging (for use during static destruction). | |
| auto | remove_workers_internal (std::size_t count, std::size_t min_workers=1) -> common::VoidResult |
| Internal method to remove workers from the pool. | |
Private Attributes | |
| std::string | thread_title_ |
| A title or name for this thread pool, useful for identification and logging. | |
| std::uint32_t | pool_instance_id_ {0} |
| Unique instance ID for this pool (for multi-pool scenarios). | |
| std::atomic< bool > | start_pool_ |
| Indicates whether the pool is currently running. | |
| std::shared_ptr< job_queue > | job_queue_ |
The shared job queue where jobs (job objects) are enqueued. | |
| std::unique_ptr< pool_queue_adapter_interface > | queue_adapter_ |
| Queue adapter for unified access to different queue types. | |
| std::vector< std::unique_ptr< thread_worker > > | workers_ |
| A collection of worker threads associated with this pool. | |
| std::mutex | workers_mutex_ |
| Mutex protecting concurrent access to the workers_ vector. | |
| thread_context | context_ |
| The thread context providing access to logging and monitoring services. | |
| cancellation_token | pool_cancellation_token_ |
| Pool-level cancellation token. | |
| std::shared_ptr< metrics::metrics_service > | metrics_service_ |
| Centralized metrics service for all pool and worker metrics. | |
| std::unique_ptr< diagnostics::thread_pool_diagnostics > | diagnostics_ |
| Diagnostics interface for this pool. | |
| std::once_flag | diagnostics_init_flag_ |
| Once flag for thread-safe lazy initialization of diagnostics_. | |
| std::vector< std::unique_ptr< pool_policy > > | policies_ |
| Registered pool policies for extending thread pool behavior. | |
| std::mutex | policies_mutex_ |
| Mutex protecting concurrent access to the policies_ vector. | |
Static Private Attributes | |
| static std::atomic< std::uint32_t > | next_pool_instance_id_ {0} |
| Static counter for generating unique pool instance IDs. | |
Friends | |
| class | numa_thread_pool |
| class | autoscaler |
| class | diagnostics::thread_pool_diagnostics |
A thread pool for concurrent execution of jobs using multiple worker threads.
The thread_pool class manages a group of worker threads that process jobs from a shared job_queue. This implementation provides:
The thread pool is designed for scenarios where many short-lived tasks need to be executed asynchronously without creating a new thread for each task.
All public methods of this class are thread-safe and can be called from any thread. The underlying job_queue is also thread-safe, allowing multiple workers to dequeue jobs concurrently.
For IExecutor compatibility, use thread_pool_executor_adapter:
Definition at line 181 of file thread_pool.h.
| kcenon::thread::thread_pool::thread_pool | ( | const std::string & | thread_title = "thread_pool", |
| const thread_context & | context = thread_context() ) |
Constructs a new thread_pool instance.
Constructs a thread pool with adaptive job queue.
| thread_title | An optional title or identifier for the thread pool (defaults to "thread_pool"). |
| context | Optional thread context for logging and monitoring (defaults to empty context). |
This title can be used for logging or debugging purposes. The context provides access to logging and monitoring services.
Implementation details:
Adaptive Queue Strategy:
| thread_title | Descriptive name for this thread pool instance |
| context | Thread context providing logging and monitoring services |
Definition at line 51 of file thread_pool.cpp.
References context_, and kcenon::thread::thread_context::monitoring().

| kcenon::thread::thread_pool::thread_pool | ( | const std::string & | thread_title, |
| std::shared_ptr< job_queue > | custom_queue, | ||
| const thread_context & | context = thread_context() ) |
Constructs a new thread_pool instance with a custom job queue.
Constructs a thread pool with a custom job queue.
| thread_title | A title or identifier for the thread pool. |
| custom_queue | A custom job queue implementation (e.g., backpressure_job_queue). |
| context | Optional thread context for logging and monitoring (defaults to empty context). |
This constructor allows injecting a specialized job queue such as backpressure_job_queue for rate limiting and flow control.
Implementation details:
| thread_title | Descriptive name for this thread pool instance |
| custom_queue | Custom job queue implementation |
| context | Thread context providing logging and monitoring services |
Definition at line 82 of file thread_pool.cpp.
References context_, and kcenon::thread::thread_context::monitoring().

| kcenon::thread::thread_pool::thread_pool | ( | const std::string & | thread_title, |
| std::unique_ptr< pool_queue_adapter_interface > | queue_adapter, | ||
| const thread_context & | context = thread_context() ) |
Constructs a new thread_pool instance with a policy_queue adapter.
Constructs a thread pool with a policy_queue adapter.
| thread_title | A title or identifier for the thread pool. |
| queue_adapter | A queue adapter wrapping a policy_queue. |
| context | Optional thread context for logging and monitoring (defaults to empty context). |
This constructor allows using the new policy-based queue system with thread_pool. Use adapters from <kcenon/thread/adapters/policy_queue_adapter.h>.
Implementation details:
| thread_title | Descriptive name for this thread pool instance |
| queue_adapter | Queue adapter wrapping job_queue or policy_queue |
| context | Thread context providing logging and monitoring services |
Definition at line 116 of file thread_pool.cpp.
References context_, and kcenon::thread::thread_context::monitoring().

|
virtual |
Virtual destructor. Cleans up resources used by the thread pool.
Destroys the thread pool, ensuring all workers are stopped.
If the pool is still running, this typically calls stop() internally to ensure all worker threads are properly shut down.
Implementation details:
Definition at line 148 of file thread_pool.cpp.
References kcenon::thread::thread_logger::is_shutting_down(), stop(), and stop_unsafe().

| void kcenon::thread::thread_pool::add_policy | ( | std::unique_ptr< pool_policy > | policy | ) |
Add a policy to the pool.
| policy | Unique pointer to the policy to add. |
Policies provide a way to extend thread pool behavior without modifying the thread_pool class. Multiple policies can be added and they will be called in order of addition.
Definition at line 834 of file thread_pool.cpp.
| auto kcenon::thread::thread_pool::check_worker_health | ( | bool | restart_failed = true | ) | -> std::size_t |
Check health of all worker threads and restart failed workers.
This method performs health monitoring on all worker threads:
Use Cases:
Thread Safety:
| restart_failed | If true, creates new workers to replace failed ones |
Example:
Definition at line 706 of file thread_pool.cpp.
|
nodiscard |
Collects diagnostics information from all workers.
Thread Safety:
Definition at line 783 of file thread_pool.cpp.
References kcenon::thread::info.
Referenced by kcenon::thread::diagnostics::thread_pool_diagnostics::detect_bottlenecks(), and kcenon::thread::diagnostics::thread_pool_diagnostics::dump_thread_states().

|
nodiscard |
Get the diagnostics interface for this pool.
The diagnostics interface provides:
Definition at line 768 of file thread_pool.cpp.
|
nodiscard |
Get the diagnostics interface for this pool (const version).
Definition at line 775 of file thread_pool.cpp.
|
nodiscard |
Access enhanced metrics (read-only reference).
| std::runtime_error | if enhanced metrics is not enabled. |
Definition at line 294 of file thread_pool.cpp.
References metrics_service_.
|
nodiscard |
Get enhanced metrics snapshot.
Returns empty snapshot if enhanced metrics is not enabled.
Definition at line 298 of file thread_pool.cpp.
References metrics_service_.
Referenced by kcenon::thread::autoscaler::collect_metrics().

| auto kcenon::thread::thread_pool::enqueue | ( | std::unique_ptr< job > && | job | ) | -> common::VoidResult |
Enqueues a new job into the shared job_queue.
Adds a single job to the thread pool for processing.
| job | A std::unique_ptr<job> representing the work to be done. |
common::VoidResult containing an error on failure, or success value on success.Implementation details:
Queue Behavior:
Thread Safety:
| job | Unique pointer to job (ownership transferred) |
Definition at line 323 of file thread_pool.cpp.
References kcenon::thread::invalid_argument, kcenon::thread::latency, kcenon::thread::queue_stopped, and kcenon::thread::resource_allocation_failed.
Referenced by main().

| auto kcenon::thread::thread_pool::enqueue | ( | std::unique_ptr< thread_worker > && | worker | ) | -> common::VoidResult |
Adds a thread_worker to the thread pool for specialized or additional processing.
| worker | A std::unique_ptr<thread_worker> object. |
common::VoidResult containing an error on failure, or success value on success. Definition at line 412 of file thread_pool.cpp.
References kcenon::thread::invalid_argument, and kcenon::thread::resource_allocation_failed.
| auto kcenon::thread::thread_pool::enqueue_batch | ( | std::vector< std::unique_ptr< job > > && | jobs | ) | -> common::VoidResult |
Enqueues a batch of jobs into the shared job_queue.
| jobs | A vector of std::unique_ptr<job> objects to be added. |
common::VoidResult containing an error on failure, or success value on success. Definition at line 368 of file thread_pool.cpp.
References kcenon::thread::invalid_argument, kcenon::thread::latency, kcenon::thread::queue_stopped, and kcenon::thread::resource_allocation_failed.
Referenced by main(), store_job(), store_job(), and store_job().

| auto kcenon::thread::thread_pool::enqueue_batch | ( | std::vector< std::unique_ptr< thread_worker > > && | workers | ) | -> common::VoidResult |
Adds a batch of thread_worker objects to the thread pool.
| workers | A vector of std::unique_ptr<thread_worker> objects. |
common::VoidResult containing an error on failure, or success value on success. Definition at line 469 of file thread_pool.cpp.
References kcenon::thread::invalid_argument, and kcenon::thread::resource_allocation_failed.
|
nodiscard |
Find a policy by name.
| name | The policy name to search for. |
Definition at line 118 of file thread_pool_impl.h.
| auto kcenon::thread::thread_pool::get_active_worker_count | ( | ) | const -> std::size_t |
Get the current number of active (running) workers.
This differs from the total worker count as it only counts workers that are actively running, not stopped or stopping.
Definition at line 755 of file thread_pool.cpp.
Referenced by kcenon::thread::autoscaler::autoscaler(), kcenon::thread::autoscaler::can_scale_down(), kcenon::thread::autoscaler::can_scale_up(), kcenon::thread::diagnostics::thread_pool_diagnostics::check_worker_health(), kcenon::thread::autoscaler::collect_metrics(), kcenon::thread::diagnostics::thread_pool_diagnostics::detect_bottlenecks(), kcenon::thread::diagnostics::thread_pool_diagnostics::format_thread_dump(), kcenon::thread::diagnostics::thread_pool_diagnostics::health_check(), and kcenon::thread::adapters::thread_pool_executor_adapter::worker_count().

|
nodiscard |
Gets the thread context for this pool.
Definition at line 645 of file thread_pool.cpp.
|
nodiscard |
Get the number of idle workers.
Definition at line 679 of file thread_pool.cpp.
References workers_, and workers_mutex_.
Referenced by kcenon::thread::diagnostics::thread_pool_diagnostics::check_worker_health(), kcenon::thread::autoscaler::collect_metrics(), kcenon::thread::diagnostics::thread_pool_diagnostics::detect_bottlenecks(), kcenon::thread::diagnostics::thread_pool_diagnostics::format_thread_dump(), and report_metrics().

|
nodiscard |
Returns the shared job_queue used by this thread pool.
Returns the shared job queue used by all workers.
std::shared_ptr<job_queue> that stores the queued jobs.The returned queue is shared among all worker threads in the pool, which can concurrently dequeue and process jobs.
Implementation details:
Definition at line 269 of file thread_pool.cpp.
Referenced by kcenon::thread::diagnostics::thread_pool_diagnostics::check_queue_health(), kcenon::thread::diagnostics::thread_pool_diagnostics::detect_bottlenecks(), and kcenon::thread::diagnostics::thread_pool_diagnostics::health_check().

| auto kcenon::thread::thread_pool::get_pending_task_count | ( | ) | const -> std::size_t |
Get the number of pending tasks in the queue.
Definition at line 695 of file thread_pool.cpp.
References job_queue_, and queue_adapter_.
Referenced by kcenon::thread::diagnostics::thread_pool_diagnostics::check_queue_health(), kcenon::thread::autoscaler::collect_metrics(), kcenon::thread::diagnostics::thread_pool_diagnostics::detect_bottlenecks(), kcenon::thread::diagnostics::thread_pool_diagnostics::health_check(), and kcenon::thread::adapters::thread_pool_executor_adapter::pending_tasks().

|
nodiscard |
Get all registered policies.
Definition at line 843 of file thread_pool.cpp.
|
nodiscard |
Get the pool instance id.
Definition at line 649 of file thread_pool.cpp.
References pool_instance_id_.
|
nodiscard |
Retrieves a std::shared_ptr to this thread_pool instance.
Returns a shared pointer to this thread pool instance.
thread_pool object.By inheriting from std::enable_shared_from_this, you can call get_ptr() within member functions to avoid storing a separate shared pointer.
Implementation details:
Definition at line 169 of file thread_pool.cpp.
|
nodiscard |
Check if enhanced metrics is enabled.
Definition at line 290 of file thread_pool.cpp.
References metrics_service_.
Referenced by kcenon::thread::autoscaler::collect_metrics().

| auto kcenon::thread::thread_pool::is_running | ( | ) | const -> bool |
Check if the thread pool is currently running.
Definition at line 689 of file thread_pool.cpp.
References start_pool_.
Referenced by kcenon::thread::diagnostics::thread_pool_diagnostics::check_worker_health(), kcenon::thread::diagnostics::thread_pool_diagnostics::is_healthy(), and kcenon::thread::adapters::thread_pool_executor_adapter::is_running().

|
nodiscardnoexcept |
Access aggregated runtime metrics (read-only reference).
Definition at line 273 of file thread_pool.cpp.
References metrics_service_.
Referenced by kcenon::thread::autoscaler::collect_metrics(), kcenon::thread::diagnostics::thread_pool_diagnostics::detect_bottlenecks(), kcenon::thread::diagnostics::thread_pool_diagnostics::health_check(), and report_metrics().

| auto kcenon::thread::thread_pool::remove_policy | ( | const std::string & | name | ) | -> bool |
Remove a policy by name.
| name | The policy name to remove. |
Definition at line 847 of file thread_pool.cpp.
|
nodiscardprivate |
Internal method to remove workers from the pool.
| count | Number of workers to remove. |
| min_workers | Minimum workers to keep. |
Used internally by autoscaler for scale-down operations. Gracefully stops and removes idle workers.
Definition at line 867 of file thread_pool.cpp.
References kcenon::thread::utils::formatter::format().

| void kcenon::thread::thread_pool::report_metrics | ( | ) |
Collect and report current thread pool metrics.
This method gathers current metrics from the pool and reports them through the monitoring interface if available.
Definition at line 653 of file thread_pool.cpp.
References context_, get_idle_worker_count(), job_queue_, metrics(), kcenon::thread::thread_context::monitoring(), pool_instance_id_, queue_adapter_, thread_title_, kcenon::thread::thread_context::update_thread_pool_metrics(), workers_, and workers_mutex_.

| void kcenon::thread::thread_pool::reset_metrics | ( | ) |
Reset accumulated metrics.
Definition at line 277 of file thread_pool.cpp.
References metrics_service_, and kcenon::thread::metrics::ThreadPoolMetrics::reset().

| void kcenon::thread::thread_pool::set_enhanced_metrics_enabled | ( | bool | enabled | ) |
Enable or disable enhanced metrics collection.
| enabled | True to enable enhanced metrics (histograms, percentiles). |
When enabled, additional metrics like latency histograms and throughput counters are collected. This has minimal overhead (< 100ns per operation) but can be disabled for maximum performance.
Definition at line 281 of file thread_pool.cpp.
References metrics_service_, workers_, and workers_mutex_.
| auto kcenon::thread::thread_pool::start | ( | void | ) | -> common::VoidResult |
Starts the thread pool and all associated workers.
Starts all worker threads in the pool.
common::VoidResult containing an error on failure, or success value on success.If the pool is already running, a subsequent call to start() may return an error. On success, each thread_worker in workers_ is started, enabling them to process jobs from the job_queue_.
Implementation details:
Startup Sequence:
Error Handling:
Definition at line 196 of file thread_pool.cpp.
References kcenon::thread::cancellation_token::create(), kcenon::thread::invalid_argument, kcenon::thread::queue_stopped, and kcenon::thread::thread_already_running.
Referenced by main(), and main().


| auto kcenon::thread::thread_pool::stop | ( | const bool & | immediately_stop = false | ) | -> common::VoidResult |
Stops the thread pool and all worker threads.
| immediately_stop | If true, any ongoing jobs may be interrupted; if false (default), each worker attempts to finish its current job before stopping. |
common::VoidResult containing an error on failure, or success value on success. Definition at line 518 of file thread_pool.cpp.
References kcenon::thread::utils::formatter::format().
Referenced by main(), main(), kcenon::thread::adapters::thread_pool_executor_adapter::shutdown(), and ~thread_pool().


|
privatenoexcept |
Stops the thread pool without logging (for use during static destruction).
This method performs the same shutdown sequence as stop() but without any logging operations. It is specifically designed to be called from the destructor when static destruction is in progress, preventing Static Destruction Order Fiasco (SDOF) by avoiding access to potentially destroyed logger/monitoring singletons.
common::VoidResult containing an error on failure, or success value on success.This method performs the same shutdown sequence as stop() but without any logging operations. It is specifically designed to be called from the destructor when static destruction is in progress, preventing Static Destruction Order Fiasco (SDOF) by avoiding access to potentially destroyed logger/monitoring singletons.
Implementation details:
result_void containing an error on failure, or success value on success. Definition at line 584 of file thread_pool.cpp.
References kcenon::thread::cancellation_token::cancel(), job_queue_, pool_cancellation_token_, queue_adapter_, start_pool_, workers_, and workers_mutex_.
Referenced by ~thread_pool().


|
nodiscard |
Definition at line 35 of file thread_pool_impl.h.
|
nodiscard |
Definition at line 60 of file thread_pool_impl.h.
References kcenon::thread::detail::batch_apply(), and kcenon::thread::submit_options::name.

|
nodiscard |
Submit a batch and wait for all results.
Convenience overload that blocks until all tasks complete. Equivalent to submit(callables, submit_options::all()).
| F | Callable type |
| R | Return type (automatically deduced) |
| callables | Vector of functions to execute |
| opts | Submit options (wait_all is implicitly true) |
Definition at line 71 of file thread_pool_impl.h.
References kcenon::thread::detail::collect_all().

|
nodiscard |
Submit a batch and return first completed result.
Convenience overload that returns when any task completes. Equivalent to submit(callables, submit_options::any()).
| F | Callable type |
| R | Return type (automatically deduced) |
| callables | Vector of functions to execute |
| opts | Submit options (wait_any is implicitly true) |
Definition at line 79 of file thread_pool_impl.h.
References kcenon::thread::completed, kcenon::thread::invalid_argument, kcenon::thread::job_execution_failed, kcenon::thread::make_error_result(), and kcenon::thread::unknown_error.

|
nodiscard |
Provides a string representation of this thread_pool.
Derived classes may override this to include more diagnostic or state-related info.
Definition at line 616 of file thread_pool.cpp.
References kcenon::thread::utils::formatter::format_to().
Referenced by std::formatter< kcenon::thread::thread_pool >::format(), std::formatter< kcenon::thread::thread_pool, wchar_t >::format(), kcenon::thread::diagnostics::thread_pool_diagnostics::format_thread_dump(), main(), and kcenon::thread::diagnostics::thread_pool_diagnostics::to_prometheus().


|
friend |
Definition at line 186 of file thread_pool.h.
|
friend |
Definition at line 643 of file thread_pool.h.
|
friend |
Definition at line 184 of file thread_pool.h.
|
private |
The thread context providing access to logging and monitoring services.
This context is shared with all worker threads created by this pool, enabling consistent logging and monitoring throughout the pool.
Definition at line 721 of file thread_pool.h.
Referenced by report_metrics(), thread_pool(), thread_pool(), and thread_pool().
|
mutableprivate |
Diagnostics interface for this pool.
Lazily initialized on first access to diagnostics().
Definition at line 776 of file thread_pool.h.
|
mutableprivate |
Once flag for thread-safe lazy initialization of diagnostics_.
Definition at line 781 of file thread_pool.h.
|
private |
The shared job queue where jobs (job objects) are enqueued.
Worker threads dequeue jobs from this queue to perform tasks. The queue persists for the lifetime of the pool or until no more references exist.
Definition at line 676 of file thread_pool.h.
Referenced by get_pending_task_count(), report_metrics(), and stop_unsafe().
|
private |
Centralized metrics service for all pool and worker metrics.
Consolidates metrics management that was previously spread across thread_pool and thread_worker. Provides:
Definition at line 768 of file thread_pool.h.
Referenced by enhanced_metrics(), enhanced_metrics_snapshot(), is_enhanced_metrics_enabled(), metrics(), reset_metrics(), and set_enhanced_metrics_enabled().
|
staticprivate |
Static counter for generating unique pool instance IDs.
Definition at line 647 of file thread_pool.h.
|
private |
Registered pool policies for extending thread pool behavior.
Policies are called in order of addition for each lifecycle event. Protected by policies_mutex_ for thread-safe access.
Definition at line 789 of file thread_pool.h.
|
mutableprivate |
Mutex protecting concurrent access to the policies_ vector.
Definition at line 794 of file thread_pool.h.
|
private |
Pool-level cancellation token.
This token is used to propagate cancellation to all workers and jobs when the pool is stopped. Each worker receives a linked token that combines this pool token with its own worker token, creating a hierarchical cancellation structure.
Cancellation Hierarchy:
Definition at line 739 of file thread_pool.h.
Referenced by stop_unsafe().
|
private |
Unique instance ID for this pool (for multi-pool scenarios).
Definition at line 657 of file thread_pool.h.
Referenced by get_pool_instance_id(), and report_metrics().
|
private |
Queue adapter for unified access to different queue types.
When set, provides a unified interface for both job_queue and policy_queue. This enables thread_pool to work with the new policy-based queue system while maintaining backward compatibility.
The adapter wraps either:
Definition at line 689 of file thread_pool.h.
Referenced by get_pending_task_count(), report_metrics(), and stop_unsafe().
|
private |
Indicates whether the pool is currently running.
Set to true after a successful call to start(), and reset to false after stop(). Used internally to prevent multiple active starts or erroneous state transitions.
Definition at line 665 of file thread_pool.h.
Referenced by is_running(), and stop_unsafe().
|
private |
A title or name for this thread pool, useful for identification and logging.
Definition at line 652 of file thread_pool.h.
Referenced by report_metrics().
|
private |
A collection of worker threads associated with this pool.
Each thread_worker typically runs in its own thread context, processing jobs from job_queue_ or performing specialized logic. They are started together when thread_pool::start() is called.
Thread Safety:
Definition at line 702 of file thread_pool.h.
Referenced by kcenon::thread::diagnostics::thread_pool_diagnostics::check_worker_health(), kcenon::thread::diagnostics::thread_pool_diagnostics::detect_bottlenecks(), kcenon::thread::diagnostics::thread_pool_diagnostics::format_thread_dump(), get_idle_worker_count(), kcenon::thread::diagnostics::thread_pool_diagnostics::health_check(), kcenon::thread::diagnostics::thread_pool_diagnostics::is_healthy(), report_metrics(), set_enhanced_metrics_enabled(), and stop_unsafe().
|
mutableprivate |
Mutex protecting concurrent access to the workers_ vector.
Synchronization Strategy:
Definition at line 713 of file thread_pool.h.
Referenced by kcenon::thread::diagnostics::thread_pool_diagnostics::check_worker_health(), kcenon::thread::diagnostics::thread_pool_diagnostics::detect_bottlenecks(), kcenon::thread::diagnostics::thread_pool_diagnostics::format_thread_dump(), get_idle_worker_count(), kcenon::thread::diagnostics::thread_pool_diagnostics::health_check(), kcenon::thread::diagnostics::thread_pool_diagnostics::is_healthy(), report_metrics(), set_enhanced_metrics_enabled(), and stop_unsafe().