|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
Fluent builder for creating and configuring thread pools. More...
#include <thread_pool_builder.h>

Public Member Functions | |
| thread_pool_builder (const std::string &name="thread_pool") | |
| Constructs a builder with the given pool name. | |
| thread_pool_builder & | with_workers (std::size_t count) |
| Sets the number of worker threads. | |
| thread_pool_builder & | with_context (const thread_context &context) |
| Sets the thread context for logging and monitoring. | |
| thread_pool_builder & | with_queue (std::shared_ptr< job_queue > queue) |
| Sets a custom job queue. | |
| thread_pool_builder & | with_queue_adapter (std::unique_ptr< pool_queue_adapter_interface > adapter) |
| Sets a policy-based queue adapter. | |
| thread_pool_builder & | with_circuit_breaker (const circuit_breaker_config &config={}) |
| Adds circuit breaker protection. | |
| thread_pool_builder & | with_circuit_breaker (std::shared_ptr< circuit_breaker > cb) |
| Adds circuit breaker with an existing circuit breaker instance. | |
| thread_pool_builder & | with_autoscaling (const autoscaling_policy &config={}) |
| Enables autoscaling with the specified policy. | |
| thread_pool_builder & | with_work_stealing () |
| Enables work-stealing with default configuration. | |
| thread_pool_builder & | with_work_stealing (const worker_policy &config) |
| Enables work-stealing with custom configuration. | |
| thread_pool_builder & | with_diagnostics () |
| Enables diagnostics for the pool. | |
| thread_pool_builder & | with_enhanced_metrics () |
| Enables enhanced metrics collection. | |
| thread_pool_builder & | with_policy (std::unique_ptr< pool_policy > policy) |
| Adds a custom policy to the pool. | |
| std::shared_ptr< thread_pool > | build () |
| Builds and returns the configured thread pool. | |
| std::shared_ptr< thread_pool > | build_and_start () |
| Builds the pool and starts it immediately. | |
Private Member Functions | |
| void | reset () |
Private Attributes | |
| std::string | name_ |
| std::size_t | worker_count_ {0} |
| thread_context | context_ |
| std::shared_ptr< job_queue > | custom_queue_ |
| std::unique_ptr< pool_queue_adapter_interface > | queue_adapter_ |
| std::vector< std::unique_ptr< pool_policy > > | policies_ |
| bool | enable_diagnostics_ {false} |
| bool | enable_enhanced_metrics_ {false} |
| std::optional< circuit_breaker_config > | circuit_breaker_config_ |
| std::shared_ptr< circuit_breaker > | shared_circuit_breaker_ |
| std::optional< autoscaling_policy > | autoscaling_config_ |
| std::optional< worker_policy > | work_stealing_config_ |
Fluent builder for creating and configuring thread pools.
The thread_pool_builder provides a fluent API for constructing thread pools with various configuration options. This pattern improves readability and makes configuration immutable until the pool is built.
with_*() methods return *this for chainingDefinition at line 77 of file thread_pool_builder.h.
|
explicit |
Constructs a builder with the given pool name.
| name | Name/title for the thread pool. |
The name is used for identification, logging, and debugging.
Definition at line 9 of file thread_pool_builder.cpp.
|
nodiscard |
Builds and returns the configured thread pool.
After calling build(), the builder is reset and can be reused to build another pool with different settings.
Definition at line 98 of file thread_pool_builder.cpp.
References autoscaling_config_, circuit_breaker_config_, context_, custom_queue_, enable_diagnostics_, enable_enhanced_metrics_, name_, policies_, queue_adapter_, reset(), shared_circuit_breaker_, work_stealing_config_, and worker_count_.
Referenced by build_and_start().


|
nodiscard |
Builds the pool and starts it immediately.
Convenience method equivalent to:
Definition at line 179 of file thread_pool_builder.cpp.
References build().
Referenced by kcenon::thread::event_bus::event_bus().


|
private |
Definition at line 186 of file thread_pool_builder.cpp.
References autoscaling_config_, circuit_breaker_config_, context_, custom_queue_, enable_diagnostics_, enable_enhanced_metrics_, name_, policies_, queue_adapter_, shared_circuit_breaker_, work_stealing_config_, and worker_count_.
Referenced by build().

| thread_pool_builder & kcenon::thread::thread_pool_builder::with_autoscaling | ( | const autoscaling_policy & | config = {} | ) |
Enables autoscaling with the specified policy.
| config | Autoscaling policy configuration. |
The autoscaler automatically adjusts worker count based on load metrics (utilization, queue depth, latency).
Definition at line 59 of file thread_pool_builder.cpp.
References autoscaling_config_.
| thread_pool_builder & kcenon::thread::thread_pool_builder::with_circuit_breaker | ( | const circuit_breaker_config & | config = {} | ) |
Adds circuit breaker protection.
| config | Circuit breaker configuration. |
The circuit breaker monitors job failures and automatically opens when a threshold is exceeded, preventing cascading failures.
Definition at line 43 of file thread_pool_builder.cpp.
References circuit_breaker_config_, and shared_circuit_breaker_.
| thread_pool_builder & kcenon::thread::thread_pool_builder::with_circuit_breaker | ( | std::shared_ptr< circuit_breaker > | cb | ) |
Adds circuit breaker with an existing circuit breaker instance.
| cb | Shared circuit breaker for multiple pools. |
Use this to share a circuit breaker across multiple pools.
Definition at line 51 of file thread_pool_builder.cpp.
References circuit_breaker_config_, and shared_circuit_breaker_.
| thread_pool_builder & kcenon::thread::thread_pool_builder::with_context | ( | const thread_context & | context | ) |
Sets the thread context for logging and monitoring.
| context | Thread context with logger and monitoring services. |
Definition at line 24 of file thread_pool_builder.cpp.
References context_.
| thread_pool_builder & kcenon::thread::thread_pool_builder::with_diagnostics | ( | ) |
Enables diagnostics for the pool.
Diagnostics provide thread dumps, job inspection, and bottleneck detection capabilities.
Definition at line 80 of file thread_pool_builder.cpp.
References enable_diagnostics_.
| thread_pool_builder & kcenon::thread::thread_pool_builder::with_enhanced_metrics | ( | ) |
Enables enhanced metrics collection.
Enhanced metrics include latency histograms, percentiles, and sliding window throughput tracking.
Definition at line 86 of file thread_pool_builder.cpp.
References enable_enhanced_metrics_.
| thread_pool_builder & kcenon::thread::thread_pool_builder::with_policy | ( | std::unique_ptr< pool_policy > | policy | ) |
Adds a custom policy to the pool.
| policy | Custom pool policy. |
Use this to add custom policies that implement pool_policy.
Definition at line 92 of file thread_pool_builder.cpp.
References policies_.
| thread_pool_builder & kcenon::thread::thread_pool_builder::with_queue | ( | std::shared_ptr< job_queue > | queue | ) |
Sets a custom job queue.
| queue | Custom job queue implementation. |
Use this to inject specialized queues like backpressure_job_queue.
Definition at line 30 of file thread_pool_builder.cpp.
References custom_queue_.
| thread_pool_builder & kcenon::thread::thread_pool_builder::with_queue_adapter | ( | std::unique_ptr< pool_queue_adapter_interface > | adapter | ) |
Sets a policy-based queue adapter.
| adapter | Queue adapter for policy_queue. |
Use this for the new policy-based queue system.
Definition at line 36 of file thread_pool_builder.cpp.
References queue_adapter_.
| thread_pool_builder & kcenon::thread::thread_pool_builder::with_work_stealing | ( | ) |
Enables work-stealing with default configuration.
Work-stealing enables idle workers to steal jobs from busy workers, improving load balancing and throughput.
Definition at line 66 of file thread_pool_builder.cpp.
References kcenon::thread::worker_policy::enable_work_stealing, and work_stealing_config_.
| thread_pool_builder & kcenon::thread::thread_pool_builder::with_work_stealing | ( | const worker_policy & | config | ) |
Enables work-stealing with custom configuration.
| config | Worker policy with work-stealing settings. |
Definition at line 74 of file thread_pool_builder.cpp.
References work_stealing_config_.
| thread_pool_builder & kcenon::thread::thread_pool_builder::with_workers | ( | std::size_t | count | ) |
Sets the number of worker threads.
| count | Number of workers to create. |
If not specified, defaults to std::thread::hardware_concurrency().
Definition at line 18 of file thread_pool_builder.cpp.
References worker_count_.
Referenced by kcenon::thread::event_bus::event_bus().

|
private |
Definition at line 248 of file thread_pool_builder.h.
Referenced by build(), reset(), and with_autoscaling().
|
private |
Definition at line 246 of file thread_pool_builder.h.
Referenced by build(), reset(), with_circuit_breaker(), and with_circuit_breaker().
|
private |
Definition at line 239 of file thread_pool_builder.h.
Referenced by build(), reset(), and with_context().
|
private |
Definition at line 240 of file thread_pool_builder.h.
Referenced by build(), reset(), and with_queue().
|
private |
Definition at line 243 of file thread_pool_builder.h.
Referenced by build(), reset(), and with_diagnostics().
|
private |
Definition at line 244 of file thread_pool_builder.h.
Referenced by build(), reset(), and with_enhanced_metrics().
|
private |
Definition at line 237 of file thread_pool_builder.h.
|
private |
Definition at line 242 of file thread_pool_builder.h.
Referenced by build(), reset(), and with_policy().
|
private |
Definition at line 241 of file thread_pool_builder.h.
Referenced by build(), reset(), and with_queue_adapter().
|
private |
Definition at line 247 of file thread_pool_builder.h.
Referenced by build(), reset(), with_circuit_breaker(), and with_circuit_breaker().
|
private |
Definition at line 249 of file thread_pool_builder.h.
Referenced by build(), reset(), with_work_stealing(), and with_work_stealing().
|
private |
Definition at line 238 of file thread_pool_builder.h.
Referenced by build(), reset(), and with_workers().