|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
A NUMA-aware thread pool optimized for Non-Uniform Memory Access architectures. More...
#include <numa_thread_pool.h>


Public Member Functions | |
| numa_thread_pool (const std::string &thread_title="numa_thread_pool", const thread_context &context=thread_context()) | |
Constructs a new numa_thread_pool instance. | |
| numa_thread_pool (const std::string &thread_title, std::shared_ptr< job_queue > custom_queue, const thread_context &context=thread_context()) | |
Constructs a new numa_thread_pool instance with a custom job queue. | |
| numa_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 numa_thread_pool instance with a policy_queue adapter. | |
| ~numa_thread_pool () override=default | |
| Virtual destructor. | |
| void | configure_numa_work_stealing (const enhanced_work_stealing_config &config) |
| Configure NUMA-aware work stealing. | |
| const enhanced_work_stealing_config & | numa_work_stealing_config () const |
| Get the current NUMA work-stealing configuration. | |
| work_stealing_stats_snapshot | numa_work_stealing_stats () const |
| Get a snapshot of NUMA work-stealing statistics. | |
| const numa_topology & | numa_topology_info () const |
| Get the detected NUMA topology. | |
| bool | is_numa_system () const |
| Check if the system has NUMA architecture. | |
| void | enable_numa_work_stealing () |
| Enable NUMA-optimized work stealing with default settings. | |
| void | disable_numa_work_stealing () |
| Disable NUMA-aware work stealing. | |
| bool | is_numa_work_stealing_enabled () const |
| Check if NUMA work stealing is currently enabled. | |
Public Member Functions inherited from 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. | |
| 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 | |
| void | ensure_topology_detected () const |
| Ensure topology is detected. | |
Private Attributes | |
| numa_topology | cached_topology_ |
| Cached NUMA topology (detected on construction) | |
| bool | topology_detected_ {false} |
| Flag indicating if topology has been detected. | |
| enhanced_work_stealing_config | enhanced_ws_config_ |
| Enhanced work-stealing configuration. | |
| std::unique_ptr< numa_work_stealer > | numa_work_stealer_ |
| NUMA-aware work stealer. | |
| worker_policy | worker_policy_ |
| Worker policy configuration. | |
A NUMA-aware thread pool optimized for Non-Uniform Memory Access architectures.
The numa_thread_pool class extends thread_pool with specialized support for NUMA (Non-Uniform Memory Access) architectures. It provides:
This class is designed for systems where memory access latency varies based on the physical location of CPUs and memory. By preferring work stealing from workers on the same NUMA node, it can significantly improve cache locality and reduce cross-node memory traffic.
Use numa_thread_pool when:
For single-socket systems or NUMA-unaware workloads, use the base thread_pool class for simpler API and lower overhead.
If you were using NUMA methods directly on thread_pool, migrate as follows:
Definition at line 93 of file numa_thread_pool.h.
|
explicit |
Constructs a new numa_thread_pool instance.
| thread_title | An optional title or identifier for the thread pool (defaults to "numa_thread_pool"). |
| context | Optional thread context for logging and monitoring (defaults to empty context). |
The pool automatically detects the system's NUMA topology on construction.
Definition at line 10 of file numa_thread_pool.cpp.
References ensure_topology_detected().

| kcenon::thread::numa_thread_pool::numa_thread_pool | ( | const std::string & | thread_title, |
| std::shared_ptr< job_queue > | custom_queue, | ||
| const thread_context & | context = thread_context() ) |
Constructs a new numa_thread_pool instance with a custom job queue.
| thread_title | A title or identifier for the thread pool. |
| custom_queue | A custom job queue implementation. |
| context | Optional thread context for logging and monitoring. |
Definition at line 17 of file numa_thread_pool.cpp.
References ensure_topology_detected().

| kcenon::thread::numa_thread_pool::numa_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 numa_thread_pool instance 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. |
Definition at line 24 of file numa_thread_pool.cpp.
References ensure_topology_detected().

|
overridedefault |
Virtual destructor.
| void kcenon::thread::numa_thread_pool::configure_numa_work_stealing | ( | const enhanced_work_stealing_config & | config | ) |
Configure NUMA-aware work stealing.
| config | The enhanced work-stealing configuration. |
This is the primary method for enabling and configuring NUMA-aware work stealing. Use factory methods on enhanced_work_stealing_config for common configurations:
Definition at line 31 of file numa_thread_pool.cpp.
References cached_topology_, kcenon::thread::worker_policy::enable_work_stealing, kcenon::thread::enhanced_work_stealing_config::enabled, enhanced_ws_config_, ensure_topology_detected(), kcenon::thread::numa_topology::node_count(), and worker_policy_.
Referenced by disable_numa_work_stealing(), and enable_numa_work_stealing().


| void kcenon::thread::numa_thread_pool::disable_numa_work_stealing | ( | ) |
Disable NUMA-aware work stealing.
Reverts to basic work stealing without NUMA awareness.
Definition at line 68 of file numa_thread_pool.cpp.
References configure_numa_work_stealing(), and kcenon::thread::enhanced_work_stealing_config::enabled.

| void kcenon::thread::numa_thread_pool::enable_numa_work_stealing | ( | ) |
Enable NUMA-optimized work stealing with default settings.
Convenience method equivalent to:
Definition at line 64 of file numa_thread_pool.cpp.
References configure_numa_work_stealing(), and kcenon::thread::enhanced_work_stealing_config::numa_optimized().

|
private |
Ensure topology is detected.
Definition at line 78 of file numa_thread_pool.cpp.
References cached_topology_, kcenon::thread::numa_topology::detect(), and topology_detected_.
Referenced by configure_numa_work_stealing(), is_numa_system(), numa_thread_pool(), numa_thread_pool(), numa_thread_pool(), and numa_topology_info().


|
nodiscard |
Check if the system has NUMA architecture.
Definition at line 59 of file numa_thread_pool.cpp.
References cached_topology_, ensure_topology_detected(), and kcenon::thread::numa_topology::is_numa_available().

|
nodiscard |
Check if NUMA work stealing is currently enabled.
Definition at line 74 of file numa_thread_pool.cpp.
References kcenon::thread::enhanced_work_stealing_config::enabled, kcenon::thread::enhanced_work_stealing_config::numa_aware, and numa_work_stealing_config().

|
nodiscard |
Get the detected NUMA topology.
The topology is detected once and cached. It includes:
Definition at line 54 of file numa_thread_pool.cpp.
References cached_topology_, and ensure_topology_detected().

|
nodiscard |
Get the current NUMA work-stealing configuration.
Definition at line 43 of file numa_thread_pool.cpp.
References enhanced_ws_config_.
Referenced by is_numa_work_stealing_enabled().

|
nodiscard |
Get a snapshot of NUMA work-stealing statistics.
Statistics include:
Definition at line 47 of file numa_thread_pool.cpp.
References numa_work_stealer_.
|
mutableprivate |
Cached NUMA topology (detected on construction)
Definition at line 210 of file numa_thread_pool.h.
Referenced by configure_numa_work_stealing(), ensure_topology_detected(), is_numa_system(), and numa_topology_info().
|
private |
Enhanced work-stealing configuration.
Definition at line 216 of file numa_thread_pool.h.
Referenced by configure_numa_work_stealing(), and numa_work_stealing_config().
|
private |
NUMA-aware work stealer.
Definition at line 219 of file numa_thread_pool.h.
Referenced by numa_work_stealing_stats().
|
mutableprivate |
Flag indicating if topology has been detected.
Definition at line 213 of file numa_thread_pool.h.
Referenced by ensure_topology_detected().
|
private |
Worker policy configuration.
Definition at line 222 of file numa_thread_pool.h.
Referenced by configure_numa_work_stealing().