13 , enable_diagnostics_(false)
14 , enable_enhanced_metrics_(false)
37 std::unique_ptr<pool_queue_adapter_interface> adapter)
44 const circuit_breaker_config& config)
52 std::shared_ptr<circuit_breaker> cb)
100 std::shared_ptr<thread_pool> pool;
116 if (worker_count == 0)
118 worker_count = std::thread::hardware_concurrency();
119 if (worker_count == 0)
125 for (std::size_t i = 0; i < worker_count; ++i)
127 auto worker = std::make_unique<thread_worker>(
true,
context_);
128 worker->set_job_queue(pool->get_job_queue());
129 pool->enqueue(std::move(worker));
134 auto cb_policy = std::make_unique<circuit_breaker_policy>(
136 pool->add_policy(std::move(cb_policy));
140 auto cb_policy = std::make_unique<circuit_breaker_policy>(
142 pool->add_policy(std::move(cb_policy));
147 auto as_policy = std::make_unique<autoscaling_pool_policy>(
149 pool->add_policy(std::move(as_policy));
154 auto ws_policy = std::make_unique<work_stealing_pool_policy>(
156 pool->add_policy(std::move(ws_policy));
161 pool->add_policy(std::move(policy));
166 pool->set_enhanced_metrics_enabled(
true);
171 (void)pool->diagnostics();
188 name_ =
"thread_pool";
Context object that provides access to optional services.
Fluent builder for creating and configuring thread pools.
std::vector< std::unique_ptr< pool_policy > > policies_
std::shared_ptr< job_queue > custom_queue_
thread_pool_builder & with_work_stealing()
Enables work-stealing with default configuration.
std::optional< autoscaling_policy > autoscaling_config_
std::unique_ptr< pool_queue_adapter_interface > queue_adapter_
thread_pool_builder & with_policy(std::unique_ptr< pool_policy > policy)
Adds a custom policy to the pool.
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.
std::shared_ptr< thread_pool > build()
Builds and returns the configured thread pool.
std::size_t worker_count_
std::optional< worker_policy > work_stealing_config_
std::shared_ptr< thread_pool > build_and_start()
Builds the pool and starts it immediately.
std::shared_ptr< circuit_breaker > shared_circuit_breaker_
thread_pool_builder & with_queue_adapter(std::unique_ptr< pool_queue_adapter_interface > adapter)
Sets a policy-based queue adapter.
thread_pool_builder & with_diagnostics()
Enables diagnostics for the pool.
bool enable_enhanced_metrics_
thread_pool_builder & with_circuit_breaker(const circuit_breaker_config &config={})
Adds circuit breaker protection.
thread_pool_builder & with_queue(std::shared_ptr< job_queue > queue)
Sets a custom job queue.
std::optional< circuit_breaker_config > circuit_breaker_config_
thread_pool_builder & with_enhanced_metrics()
Enables enhanced metrics collection.
thread_pool_builder(const std::string &name="thread_pool")
Constructs a builder with the given pool name.
thread_pool_builder & with_autoscaling(const autoscaling_policy &config={})
Enables autoscaling with the specified policy.
Core threading foundation of the thread system library.
Configuration for autoscaling behavior.
Worker behavior policy configuration.
bool enable_work_stealing
Fluent builder for creating and configuring thread pools.