Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
thread_pool_builder.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
12#pragma once
13
20#include <kcenon/common/resilience/circuit_breaker_config.h>
22
23#include <memory>
24#include <optional>
25#include <string>
26#include <thread>
27#include <vector>
28
29namespace kcenon::thread
30{
78 {
79 public:
86 explicit thread_pool_builder(const std::string& name = "thread_pool");
87
95 thread_pool_builder& with_workers(std::size_t count);
96
103
111 thread_pool_builder& with_queue(std::shared_ptr<job_queue> queue);
112
121 std::unique_ptr<pool_queue_adapter_interface> adapter);
122
135 const circuit_breaker_config& config = {});
136
145 std::shared_ptr<circuit_breaker> cb);
146
159 const autoscaling_policy& config = {});
160
171
181
190
199
209 thread_pool_builder& with_policy(std::unique_ptr<pool_policy> policy);
210
221 [[nodiscard]] std::shared_ptr<thread_pool> build();
222
234 [[nodiscard]] std::shared_ptr<thread_pool> build_and_start();
235
236 private:
237 std::string name_;
238 std::size_t worker_count_{0};
240 std::shared_ptr<job_queue> custom_queue_;
241 std::unique_ptr<pool_queue_adapter_interface> queue_adapter_;
242 std::vector<std::unique_ptr<pool_policy>> policies_;
245
246 std::optional<circuit_breaker_config> circuit_breaker_config_;
247 std::shared_ptr<circuit_breaker> shared_circuit_breaker_;
248 std::optional<autoscaling_policy> autoscaling_config_;
249 std::optional<worker_policy> work_stealing_config_;
250
251 void reset();
252 };
253
254} // namespace kcenon::thread
Configuration for autoscaling behavior and thresholds.
Pool policy implementing automatic worker scaling based on load.
Pool policy implementing circuit breaker pattern for failure protection.
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::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.
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 thread pool implementation with work stealing and auto-scaling.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
Configuration for autoscaling behavior.
Worker behavior policy configuration.
Context object providing access to optional thread system services.
Pool policy implementing work-stealing for load balancing.
Worker behavior policies and configuration.