21#include "logger/core/logger.h"
40 log_module::set_title(
"thread_pool_sample");
47 log_module::message_callback(
48 [](
const log_module::log_types& type,
const std::string& datetime,
49 const std::string& message)
53 log_module::set_wake_interval(std::chrono::milliseconds(
wait_interval_));
56 return log_module::start();
60 -> std::tuple<std::shared_ptr<thread_pool>, kcenon::common::VoidResult>
62 std::shared_ptr<thread_pool> pool;
66 pool = std::make_shared<thread_pool>();
68 catch (
const std::bad_alloc& e)
70 return {
nullptr, kcenon::common::error_info{
71 kcenon::thread::error_code::allocation_failed,
73 "thread_pool_sample"} };
76 std::vector<std::unique_ptr<thread_worker>> workers;
77 workers.reserve(worker_counts);
78 for (uint16_t i = 0; i < worker_counts; ++i)
80 workers.push_back(std::make_unique<thread_worker>());
83 auto enqueue_result = pool->enqueue_batch(std::move(workers));
84 if (enqueue_result.is_err())
86 return {
nullptr, enqueue_result.error() };
89 return { pool, kcenon::common::ok() };
94 std::vector<std::unique_ptr<job>> jobs;
99 jobs.push_back(std::make_unique<callback_job>(
100 [index](
void) -> kcenon::common::VoidResult
102 log_module::write_debug(
"Hello, World!: {}", index);
103 return kcenon::common::ok();
115 return kcenon::common::ok();
121 if (error_message.has_value())
124 error_message.value_or(
"unknown error"));
128 std::shared_ptr<thread_pool>
thread_pool =
nullptr;
129 kcenon::common::VoidResult pool_init_result;
131 if (pool_init_result.is_err())
133 log_module::write_error(
"error creating thread pool: {}",
134 pool_init_result.error().message);
142 if (store_result.is_err())
144 log_module::write_error(
"error storing job: {}", store_result.error().message);
152 if (start_result.is_err())
154 log_module::write_error(
"error starting thread pool: {}",
155 start_result.error().message);
165 if (stop_result.is_err())
167 log_module::write_error(
"error stopping thread pool: {}",
168 stop_result.error().message);
A template class representing either a value or an error.
A thread pool for concurrent execution of jobs using multiple worker threads.
auto to_string(void) const -> std::string
Provides a string representation of this thread_pool.
auto enqueue_batch(std::vector< std::unique_ptr< job > > &&jobs) -> common::VoidResult
Enqueues a batch of jobs into the shared job_queue.
auto stop(const bool &immediately_stop=false) -> common::VoidResult
Stops the thread pool and all worker threads.
auto start(void) -> common::VoidResult
Starts the thread pool and all associated workers.
Core thread pool implementation with work stealing and auto-scaling.
Core threading foundation of the thread system library.
log_module::log_types file_target_
auto store_job(std::shared_ptr< thread_pool > thread_pool) -> kcenon::common::VoidResult
log_module::log_types callback_target_
auto initialize_logger() -> std::optional< std::string >
uint32_t test_line_count_
log_module::log_types console_target_
auto create_default(const uint16_t &worker_counts) -> std::tuple< std::shared_ptr< thread_pool >, kcenon::common::VoidResult >