14#include <kcenon/thread/core/thread_pool.h>
15#include <kcenon/thread/core/thread_worker.h>
16#include <kcenon/thread/core/job_builder.h>
17#include <kcenon/thread/interfaces/thread_context.h>
32 config_.min_threads = 1;
35 config_.max_threads = config_.min_threads;
39 kcenon::thread::thread_context context;
44 std::shared_ptr<kcenon::thread::thread_pool> pool)
45 : pool_(std::move(pool)), initialized_(true) {
47 throw std::invalid_argument(
"Thread pool cannot be null");
60 std::lock_guard<std::mutex> lock(mutex_);
62 if (initialized_ && pool_ && pool_->is_running()) {
68 kcenon::thread::thread_context context;
69 pool_ = std::make_shared<kcenon::thread::thread_pool>(config_.pool_name, context);
73 std::vector<std::unique_ptr<kcenon::thread::thread_worker>> workers;
74 workers.reserve(config_.min_threads);
76 kcenon::thread::thread_context context;
77 for (std::size_t i = 0; i < config_.min_threads; ++i) {
78 workers.push_back(std::make_unique<kcenon::thread::thread_worker>(
false, context));
82 auto enqueue_result = pool_->enqueue_batch(std::move(workers));
83 if (enqueue_result.is_err()) {
88 auto start_result = pool_->start();
89 if (start_result.is_err()) {
98 std::lock_guard<std::mutex> lock(
mutex_);
103 std::lock_guard<std::mutex> lock(
mutex_);
106 pool_->stop(!wait_for_completion);
117 -> std::future<void> {
118 auto promise = std::make_shared<std::promise<void>>();
119 auto future = promise->get_future();
122 [task = std::move(task), promise]()
mutable {
125 promise->set_value();
127 promise->set_exception(std::current_exception());
137 std::function<
void()> task) -> std::future<void> {
138 auto promise = std::make_shared<std::promise<void>>();
139 auto future = promise->get_future();
142 [task = std::move(task), promise]()
mutable {
145 promise->set_value();
147 promise->set_exception(std::current_exception());
160 std::function<
void()> task,
165 throw std::runtime_error(
"Failed to start thread pool");
169 std::lock_guard<std::mutex> lock(
mutex_);
176 auto job = kcenon::thread::job_builder()
177 .name(
"pacs_pool_task")
178 .work([task = std::move(task)]() -> kcenon::common::VoidResult {
180 return kcenon::common::ok();
184 auto result =
pool_->enqueue(std::move(job));
185 if (result.is_err()) {
186 throw std::runtime_error(
"Failed to enqueue task to thread pool");
188 }
catch (
const std::exception&) {
189 throw std::runtime_error(
"Failed to enqueue task to thread pool");
199 std::lock_guard<std::mutex> lock(
mutex_);
206 std::lock_guard<std::mutex> lock(
mutex_);
207 return pool_ ?
pool_->get_pending_task_count() : 0;
211 std::lock_guard<std::mutex> lock(
mutex_);
212 return pool_ ?
pool_->get_idle_worker_count() : 0;
220 -> std::shared_ptr<
kcenon::thread::thread_pool> {
221 std::lock_guard<std::mutex> lock(
mutex_);
if(!color.empty()) style.color
void submit_internal(std::function< void()> task, job_priority priority)
Internal task submission with priority.
thread_pool_adapter(const thread_pool_config &config)
Construct adapter with configuration.
auto submit(std::function< void()> task) -> std::future< void > override
Submit a task for execution.
auto get_idle_worker_count() const -> std::size_t override
Get the number of idle workers.
auto get_underlying_pool() const -> std::shared_ptr< kcenon::thread::thread_pool >
Get the underlying thread pool.
auto get_thread_count() const -> std::size_t override
Get the current number of worker threads.
thread_pool_config config_
auto get_config() const noexcept -> const thread_pool_config &
Get the current configuration.
auto get_pending_task_count() const -> std::size_t override
Get the number of pending tasks in the queue.
void submit_fire_and_forget(std::function< void()> task) override
Submit a task without waiting for completion.
~thread_pool_adapter() override
Destructor.
auto submit_with_priority(job_priority priority, std::function< void()> task) -> std::future< void > override
Submit a task with a specific priority level.
void shutdown(bool wait_for_completion=true) override
Shutdown the thread pool.
auto start() -> bool override
Start the thread pool.
std::shared_ptr< kcenon::thread::thread_pool > pool_
auto is_running() const noexcept -> bool override
Check if the thread pool is running.
job_priority
Priority levels for job scheduling.
@ low
Background tasks (cleanup, maintenance)
Configuration options for the thread pool.
std::string pool_name
Thread pool name for logging.
std::size_t min_threads
Minimum number of worker threads.
std::size_t max_threads
Maximum number of worker threads.
Concrete implementation of thread_pool_interface using kcenon::thread.