Demonstrates priority scheduling with the typed_thread_pool. Workers are assigned to RealTime, Batch, or Background priority lanes, and jobs are routed to matching workers automatically.
#include <iostream>
#include <memory>
#include <chrono>
#include <thread>
#include "logger/core/logger.h"
#include <format>
log_module::log_types
file_target_ = log_module::log_types::None;
log_module::log_types
console_target_ = log_module::log_types::Information;
{
log_module::set_title("typed_thread_pool_sample");
log_module::message_callback(
[](const log_module::log_types& type, const std::string& datetime,
const std::string& message)
{ std::cout << formatter::format("[{}][{}] {}\n", datetime, type, message); });
{
log_module::set_wake_interval(std::chrono::milliseconds(
wait_interval_));
}
return log_module::start();
}
const uint16_t& normal_priority_workers,
const uint16_t& low_priority_workers)
-> std::tuple<std::shared_ptr<typed_thread_pool>, std::optional<std::string>>
{
std::shared_ptr<typed_thread_pool> pool;
try
{
pool = std::make_shared<typed_thread_pool>();
}
catch (const std::bad_alloc& e)
{
return { nullptr, std::string(e.what()) };
}
std::optional<std::string> error_message = std::nullopt;
std::vector<std::unique_ptr<typed_thread_worker>> workers;
workers.reserve(high_priority_workers + normal_priority_workers + low_priority_workers);
for (uint16_t i = 0; i < high_priority_workers; ++i)
{
workers.push_back(std::make_unique<typed_thread_worker>(
std::vector<job_types>{ job_types::RealTime }, "high priority worker"));
}
for (uint16_t i = 0; i < normal_priority_workers; ++i)
{
workers.push_back(std::make_unique<typed_thread_worker>(
std::vector<job_types>{ job_types::Batch }, "normal priority worker"));
}
for (uint16_t i = 0; i < low_priority_workers; ++i)
{
workers.push_back(std::make_unique<typed_thread_worker>(
std::vector<job_types>{ job_types::Background }, "low priority worker"));
}
auto enqueue_result = pool->enqueue_batch(std::move(workers));
if (enqueue_result.is_err())
{
return { nullptr, formatter::format("cannot enqueue to workers: {}",
enqueue_result.error().message) };
}
return { pool, std::nullopt };
}
{
int target = 0;
std::vector<std::unique_ptr<typed_job>> jobs;
{
target = index % 3;
jobs.push_back(std::make_unique<callback_typed_job>(
[target](void) -> kcenon::common::VoidResult
{
log_module::write_debug("Hello, World!: {} priority", target);
return kcenon::common::ok();
},
}
if (enqueue_result.is_err())
{
return formatter::format("error enqueuing jobs: {}",
enqueue_result.error().message);
}
return std::nullopt;
}
{
if (error_message.has_value())
{
std::cerr << formatter::format("error starting logger: {}\n",
error_message.value_or("unknown error"));
return 0;
}
std::shared_ptr<typed_thread_pool>
thread_pool =
nullptr;
if (error_message.has_value())
{
log_module::write_error("error creating thread pool: {}",
error_message.value_or("unknown error"));
return 0;
}
if (error_message.has_value())
{
log_module::write_error("error storing job: {}", error_message.value_or("unknown error"));
return 0;
}
if (start_result.is_err())
{
log_module::write_error("error starting thread pool: {}",
start_result.error().message);
return 0;
}
{
if (stop_result.is_err()) {
log_module::write_error("error stopping thread pool: {}", stop_result.error().message);
}
}
log_module::stop();
return 0;
}
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.
Type-based thread pool with priority scheduling and job type routing.
log_module::log_types file_target_
log_module::log_types callback_target_
auto initialize_logger() -> std::optional< std::string >
uint32_t test_line_count_
log_module::log_types console_target_
Core threading foundation of the thread system library.
job_types
Defines different types of jobs for a typed thread pool.
auto store_job(std::shared_ptr< thread_pool > thread_pool) -> kcenon::common::VoidResult
auto create_default(const uint16_t &worker_counts) -> std::tuple< std::shared_ptr< thread_pool >, kcenon::common::VoidResult >
uint16_t high_priority_workers_
uint16_t low_priority_workers_
uint16_t normal_priority_workers_