52#if __has_include(<kcenon/common/config/feature_flags.h>)
53#include <kcenon/common/config/feature_flags.h>
59#ifndef KCENON_HAS_COMMON_EXECUTOR
60#define KCENON_HAS_COMMON_EXECUTOR 0
63#if KCENON_HAS_COMMON_EXECUTOR
64#include <kcenon/common/interfaces/executor_interface.h>
82 class thread_pool_diagnostics;
126#if KCENON_HAS_COMMON_EXECUTOR
127 namespace common_ns = common;
181 class thread_pool :
public std::enable_shared_from_this<thread_pool>
198 thread_pool(
const std::string& thread_title =
"thread_pool",
211 std::shared_ptr<job_queue> custom_queue,
239 std::unique_ptr<pool_queue_adapter_interface> queue_adapter,
257 [[nodiscard]]
auto get_ptr(
void) -> std::shared_ptr<thread_pool>;
267 auto start(
void) -> common::VoidResult;
276 [[nodiscard]]
auto get_job_queue(
void) -> std::shared_ptr<job_queue>;
346 -> common::VoidResult;
354 auto
stop(const
bool& immediately_stop = false) -> common::VoidResult;
362 [[nodiscard]] auto
to_string(
void) const ->
std::
string;
423 template<typename F, typename R =
std::invoke_result_t<
std::decay_t<F>>>
460 template<
typename F,
typename R = std::invoke_result_t<std::decay_t<F>>>
462 -> std::vector<std::future<R>>;
476 template<
typename F,
typename R = std::invoke_result_t<std::decay_t<F>>>
478 const submit_options& opts = {})
494 template<
typename F,
typename R = std::invoke_result_t<std::decay_t<F>>>
496 const submit_options& opts = {})
497 -> common::Result<R>;
582 [[nodiscard]] auto
get_policies() const -> const
std::vector<
std::unique_ptr<pool_policy>>&;
597 template<typename T = pool_policy>
702 std::vector<std::unique_ptr<thread_worker>>
workers_;
806 -> common::VoidResult;
816#include <kcenon/thread/core/thread_pool_impl.h>
825#define KCENON_THREAD_INTERNAL_INCLUDE
827#undef KCENON_THREAD_INTERNAL_INCLUDE
Manages automatic scaling of thread pool workers based on load metrics.
Provides a mechanism for cooperative cancellation of operations.
Represents a unit of work (task) to be executed, typically by a job queue.
Lightweight metrics container shared between thread_pool and workers.
A NUMA-aware thread pool optimized for Non-Uniform Memory Access architectures.
Lock-free synchronization policy using Michael-Scott algorithm.
Synchronization policy using mutex and condition variable.
Policy that rejects new items when queue is full.
Policy that allows unlimited queue size.
Policy-based queue template.
Base interface for thread pool policies.
Context object that provides access to optional services.
A thread pool for concurrent execution of jobs using multiple worker threads.
static std::atomic< std::uint32_t > next_pool_instance_id_
Static counter for generating unique pool instance IDs.
auto submit_wait_any(std::vector< F > &&callables, const submit_options &opts={}) -> common::Result< R >
Submit a batch and return first completed result.
bool is_enhanced_metrics_enabled() const
Check if enhanced metrics is enabled.
virtual ~thread_pool(void)
Virtual destructor. Cleans up resources used by the thread pool.
auto diagnostics() -> diagnostics::thread_pool_diagnostics &
Get the diagnostics interface for this pool.
auto get_pending_task_count() const -> std::size_t
Get the number of pending tasks in the queue.
metrics::EnhancedSnapshot enhanced_metrics_snapshot() const
Get enhanced metrics snapshot.
void report_metrics()
Collect and report current thread pool metrics.
std::size_t get_idle_worker_count() const
Get the number of idle workers.
thread_pool(const std::string &thread_title="thread_pool", const thread_context &context=thread_context())
Constructs a new thread_pool instance.
auto to_string(void) const -> std::string
Provides a string representation of this thread_pool.
std::unique_ptr< pool_queue_adapter_interface > queue_adapter_
Queue adapter for unified access to different queue types.
auto get_context(void) const -> const thread_context &
Gets the thread context for this pool.
auto get_active_worker_count() const -> std::size_t
Get the current number of active (running) workers.
std::shared_ptr< metrics::metrics_service > metrics_service_
Centralized metrics service for all pool and worker metrics.
friend class diagnostics::thread_pool_diagnostics
std::vector< std::unique_ptr< thread_worker > > workers_
A collection of worker threads associated with this pool.
void reset_metrics()
Reset accumulated metrics.
std::mutex policies_mutex_
Mutex protecting concurrent access to the policies_ vector.
const metrics::EnhancedThreadPoolMetrics & enhanced_metrics() const
Access enhanced metrics (read-only reference).
auto submit_wait_all(std::vector< F > &&callables, const submit_options &opts={}) -> std::vector< R >
Submit a batch and wait for all results.
auto enqueue(std::unique_ptr< job > &&job) -> common::VoidResult
Enqueues a new job into the shared job_queue.
std::once_flag diagnostics_init_flag_
Once flag for thread-safe lazy initialization of diagnostics_.
auto remove_policy(const std::string &name) -> bool
Remove a policy by name.
auto collect_worker_diagnostics() const -> std::vector< diagnostics::thread_info >
Collects diagnostics information from all workers.
void add_policy(std::unique_ptr< pool_policy > policy)
Add a policy to the pool.
auto is_running() const -> bool
Check if the thread pool is currently running.
auto enqueue_batch(std::vector< std::unique_ptr< job > > &&jobs) -> common::VoidResult
Enqueues a batch of jobs into the shared job_queue.
std::string thread_title_
A title or name for this thread pool, useful for identification and logging.
std::uint32_t get_pool_instance_id() const
Get the pool instance id.
auto find_policy(const std::string &name) -> T *
Find a policy by name.
std::unique_ptr< diagnostics::thread_pool_diagnostics > diagnostics_
Diagnostics interface for this pool.
auto stop_unsafe() noexcept -> common::VoidResult
Stops the thread pool without logging (for use during static destruction).
void set_enhanced_metrics_enabled(bool enabled)
Enable or disable enhanced metrics collection.
std::atomic< bool > start_pool_
Indicates whether the pool is currently running.
auto submit(F &&callable, const submit_options &opts={}) -> std::future< R >
cancellation_token pool_cancellation_token_
Pool-level cancellation token.
const metrics::ThreadPoolMetrics & metrics() const noexcept
Access aggregated runtime metrics (read-only reference).
auto get_policies() const -> const std::vector< std::unique_ptr< pool_policy > > &
Get all registered policies.
std::vector< std::unique_ptr< pool_policy > > policies_
Registered pool policies for extending thread pool behavior.
std::shared_ptr< job_queue > job_queue_
The shared job queue where jobs (job objects) are enqueued.
auto check_worker_health(bool restart_failed=true) -> std::size_t
Check health of all worker threads and restart failed workers.
auto remove_workers_internal(std::size_t count, std::size_t min_workers=1) -> common::VoidResult
Internal method to remove workers from the pool.
std::uint32_t pool_instance_id_
Unique instance ID for this pool (for multi-pool scenarios).
thread_context context_
The thread context providing access to logging and monitoring services.
auto get_job_queue(void) -> std::shared_ptr< job_queue >
Returns the shared job_queue used by this thread pool.
auto stop(const bool &immediately_stop=false) -> common::VoidResult
Stops the thread pool and all worker threads.
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.
std::mutex workers_mutex_
Mutex protecting concurrent access to the workers_ vector.
A specialized worker thread that processes jobs from a job_queue.
Central configuration for thread_pool module.
Enhanced metrics snapshot with latency percentiles and throughput.
Forward declarations for thread system types.
Implementation of a cancellation token for cooperative cancellation.
String encoding conversion, Base64 encoding/decoding utilities.
Thread-safe FIFO job queue with optional bounded size.
Centralized metrics service for thread pool metrics management.
Core threading foundation of the thread system library.
Base interface for extensible thread pool behavior policies.
Abstract interface for queue adapters used by thread_pool.
Options for submitting jobs to the thread pool.
Options struct for unified submit() API.
Context object providing access to optional thread system services.
std::formatter specializations for thread_pool
Lightweight metrics container shared between thread_pool and workers.
Specialized worker thread that processes jobs from a job_queue.