Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
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
5#pragma once
6
15#include "forward_declarations.h"
16#include "config.h"
17#include "job_types.h"
18#include <memory>
19#include <vector>
20#include <string>
21
22namespace kcenon::thread {
23
31 template<detail::JobType job_type = job_types>
33 public:
38
44 typed_thread_pool_builder& with_title(const std::string& title) {
45 title_ = title;
46 return *this;
47 }
48
55 worker_count_ = count;
56 return *this;
57 }
58
65 queue_size_ = size;
66 return *this;
67 }
68
74 typed_thread_pool_builder& with_job_types(const std::vector<job_type>& types) {
75 job_types_ = types;
76 return *this;
77 }
78
85 use_time_tag_ = enable;
86 return *this;
87 }
88
95 auto_start_ = auto_start;
96 return *this;
97 }
98
103 [[nodiscard]] bool validate() const {
106 queue_size_ > 0 &&
107 !title_.empty();
108 }
109
115 [[nodiscard]] std::shared_ptr<typed_thread_pool_t<job_type>> build() {
116 if (!validate()) {
117 throw std::invalid_argument("Invalid thread pool configuration");
118 }
119
120 auto pool = std::make_shared<typed_thread_pool_t<job_type>>(title_);
121
122 // Create workers with specified job types
123 for (size_t i = 0; i < worker_count_; ++i) {
124 auto worker = std::make_unique<typed_thread_worker_t<job_type>>(
126 );
127 pool->enqueue(std::move(worker));
128 }
129
130 if (auto_start_) {
131 auto result = pool->start();
132 if (result.has_error()) {
133 throw std::runtime_error("Failed to start thread pool: " +
135 }
136 }
137
138 return pool;
139 }
140
146 title_ = "typed_thread_pool";
149 job_types_.clear();
150 use_time_tag_ = true;
151 auto_start_ = false;
152 return *this;
153 }
154
155 private:
156 std::string title_ = "typed_thread_pool";
159 std::vector<job_type> job_types_;
160 bool use_time_tag_ = true;
161 bool auto_start_ = false;
162 };
163
168 template<detail::JobType job_type = job_types>
172
173} // namespace kcenon::thread
const std::string & message() const noexcept
Gets the error message.
A template class representing either a value or an error.
error & get_error() &
Gets the error.
Typed thread pool builder template.
bool validate() const
Validate the current configuration.
typed_thread_pool_builder()=default
Construct a new builder with default settings.
typed_thread_pool_builder & with_time_tagging(bool enable)
Enable or disable time tagging for workers.
typed_thread_pool_builder & with_auto_start(bool auto_start)
Enable or disable automatic pool startup.
typed_thread_pool_builder & reset()
Reset the builder to default values.
typed_thread_pool_builder & with_worker_count(size_t count)
Set the number of worker threads.
typed_thread_pool_builder & with_title(const std::string &title)
Set the title/name for the thread pool.
typed_thread_pool_builder & with_job_types(const std::vector< job_type > &types)
Set the job types that workers should handle.
typed_thread_pool_builder & with_queue_size(size_t size)
Set the queue size for the thread pool.
std::shared_ptr< typed_thread_pool_t< job_type > > build()
Build the typed thread pool with current configuration.
Central configuration for typed_thread_pool module.
Job type definitions for the typed thread pool.
constexpr size_t default_queue_size
Definition config.h:26
constexpr size_t max_workers
Definition config.h:26
constexpr size_t min_workers
Definition config.h:27
constexpr size_t default_worker_count
Definition config.h:25
Core threading foundation of the thread system library.
Definition thread_impl.h:17
typed_thread_pool_builder< job_type > create_pool_builder()
Convenience function to create a builder.