45 log_module::set_title(
"typed_thread_pool_sample");
52 log_module::message_callback(
53 [](
const log_module::log_types& type,
const std::string& datetime,
54 const std::string& message)
58 log_module::set_wake_interval(std::chrono::milliseconds(
wait_interval_));
61 return log_module::start();
65 const uint16_t& normal_priority_workers,
66 const uint16_t& low_priority_workers)
67 -> std::tuple<std::shared_ptr<typed_thread_pool>, std::optional<std::string>>
69 std::shared_ptr<typed_thread_pool> pool;
73 pool = std::make_shared<typed_thread_pool>();
75 catch (
const std::bad_alloc& e)
77 return {
nullptr, std::string(e.what()) };
80 std::optional<std::string> error_message = std::nullopt;
82 std::vector<std::unique_ptr<typed_thread_worker>> workers;
83 workers.reserve(high_priority_workers + normal_priority_workers + low_priority_workers);
84 for (uint16_t i = 0; i < high_priority_workers; ++i)
86 workers.push_back(std::make_unique<typed_thread_worker>(
87 std::vector<job_types>{ job_types::RealTime },
"high priority worker"));
90 for (uint16_t i = 0; i < normal_priority_workers; ++i)
92 workers.push_back(std::make_unique<typed_thread_worker>(
93 std::vector<job_types>{ job_types::Batch },
"normal priority worker"));
96 for (uint16_t i = 0; i < low_priority_workers; ++i)
98 workers.push_back(std::make_unique<typed_thread_worker>(
99 std::vector<job_types>{ job_types::Background },
"low priority worker"));
102 auto enqueue_result = pool->enqueue_batch(std::move(workers));
103 if (enqueue_result.is_err())
106 enqueue_result.error().message) };
109 return { pool, std::nullopt };
116 std::vector<std::unique_ptr<typed_job>> jobs;
122 jobs.push_back(std::make_unique<callback_typed_job>(
123 [target](
void) -> kcenon::common::VoidResult
125 log_module::write_debug(
"Hello, World!: {} priority", target);
126 return kcenon::common::ok();
132 if (enqueue_result.is_err())
135 enqueue_result.error().message);
146 if (error_message.has_value())
149 error_message.value_or(
"unknown error"));
153 std::shared_ptr<typed_thread_pool>
thread_pool =
nullptr;
156 if (error_message.has_value())
158 log_module::write_error(
"error creating thread pool: {}",
159 error_message.value_or(
"unknown error"));
167 if (error_message.has_value())
169 log_module::write_error(
"error storing job: {}", error_message.value_or(
"unknown error"));
177 if (start_result.is_err())
179 log_module::write_error(
"error starting thread pool: {}",
180 start_result.error().message);
191 if (stop_result.is_err()) {
192 log_module::write_error(
"error stopping thread pool: {}", stop_result.error().message);
auto create_default(const uint16_t &high_priority_workers, const uint16_t &normal_priority_workers, const uint16_t &low_priority_workers) -> std::tuple< std::shared_ptr< typed_thread_pool >, std::optional< std::string > >