Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
thread_pool_sample.cpp File Reference

Thread pool sample with logger integration and batch job submission. More...

#include <iostream>
#include <memory>
#include <chrono>
#include "logger/core/logger.h"
#include <kcenon/thread/utils/formatter.h>
#include <kcenon/thread/core/thread_pool.h>
Include dependency graph for thread_pool_sample.cpp:

Go to the source code of this file.

Functions

auto initialize_logger () -> std::optional< std::string >
 
auto create_default (const uint16_t &worker_counts) -> std::tuple< std::shared_ptr< thread_pool >, kcenon::common::VoidResult >
 
auto store_job (std::shared_ptr< thread_pool > thread_pool) -> kcenon::common::VoidResult
 
auto main () -> int
 

Variables

bool use_backup_ = false
 
uint32_t max_lines_ = 0
 
uint16_t wait_interval_ = 100
 
uint32_t test_line_count_ = 1000000
 
log_module::log_types file_target_ = log_module::log_types::None
 
log_module::log_types console_target_ = log_module::log_types::Information
 
log_module::log_types callback_target_ = log_module::log_types::None
 
uint16_t thread_counts_ = 10
 

Detailed Description

Thread pool sample with logger integration and batch job submission.

Definition in file thread_pool_sample.cpp.

Function Documentation

◆ create_default()

auto create_default ( const uint16_t & worker_counts) -> std::tuple<std::shared_ptr<thread_pool>, kcenon::common::VoidResult>
Examples
thread_pool_sample.cpp, typed_thread_pool_sample.cpp, and typed_thread_pool_sample_2.cpp.

Definition at line 59 of file thread_pool_sample.cpp.

61{
62 std::shared_ptr<thread_pool> pool;
63
64 try
65 {
66 pool = std::make_shared<thread_pool>();
67 }
68 catch (const std::bad_alloc& e)
69 {
70 return { nullptr, kcenon::common::error_info{
71 kcenon::thread::error_code::allocation_failed,
72 e.what(),
73 "thread_pool_sample"} };
74 }
75
76 std::vector<std::unique_ptr<thread_worker>> workers;
77 workers.reserve(worker_counts);
78 for (uint16_t i = 0; i < worker_counts; ++i)
79 {
80 workers.push_back(std::make_unique<thread_worker>());
81 }
82
83 auto enqueue_result = pool->enqueue_batch(std::move(workers));
84 if (enqueue_result.is_err())
85 {
86 return { nullptr, enqueue_result.error() };
87 }
88
89 return { pool, kcenon::common::ok() };
90}

Referenced by main().

Here is the caller graph for this function:

◆ initialize_logger()

auto initialize_logger ( ) -> std::optional<std::string>

Definition at line 38 of file thread_pool_sample.cpp.

39{
40 log_module::set_title("thread_pool_sample");
41 log_module::set_use_backup(use_backup_);
42 log_module::set_max_lines(max_lines_);
43 log_module::file_target(file_target_);
44 log_module::console_target(console_target_);
45 log_module::callback_target(callback_target_);
46 // Note: This demonstrates the logger callback feature - std::cout is intentionally used here
47 log_module::message_callback(
48 [](const log_module::log_types& type, const std::string& datetime,
49 const std::string& message)
50 { std::cout << formatter::format("[{}][{}] {}\n", datetime, type, message); });
51 if (wait_interval_ > 0)
52 {
53 log_module::set_wake_interval(std::chrono::milliseconds(wait_interval_));
54 }
55
56 return log_module::start();
57}
static auto format(const char *formats, const FormatArgs &... args) -> std::string
Formats a narrow-character string with the given arguments.
Definition formatter.h:132
log_module::log_types file_target_
log_module::log_types callback_target_
uint32_t max_lines_
bool use_backup_
log_module::log_types console_target_
uint16_t wait_interval_

References callback_target_, console_target_, file_target_, kcenon::thread::utils::formatter::format(), max_lines_, use_backup_, and wait_interval_.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

auto main ( ) -> int

Definition at line 118 of file thread_pool_sample.cpp.

119{
120 auto error_message = initialize_logger();
121 if (error_message.has_value())
122 {
123 std::cerr << formatter::format("error starting logger: {}\n",
124 error_message.value_or("unknown error"));
125 return 0;
126 }
127
128 std::shared_ptr<thread_pool> thread_pool = nullptr;
129 kcenon::common::VoidResult pool_init_result;
130 std::tie(thread_pool, pool_init_result) = create_default(thread_counts_);
131 if (pool_init_result.is_err())
132 {
133 log_module::write_error("error creating thread pool: {}",
134 pool_init_result.error().message);
135
136 return 0;
137 }
138
139 log_module::write_information("created {}", thread_pool->to_string());
140
141 auto store_result = store_job(thread_pool);
142 if (store_result.is_err())
143 {
144 log_module::write_error("error storing job: {}", store_result.error().message);
145
146 thread_pool.reset();
147
148 return 0;
149 }
150
151 auto start_result = thread_pool->start();
152 if (start_result.is_err())
153 {
154 log_module::write_error("error starting thread pool: {}",
155 start_result.error().message);
156
157 thread_pool.reset();
158
159 return 0;
160 }
161
162 log_module::write_information("started {}", thread_pool->to_string());
163
164 auto stop_result = thread_pool->stop();
165 if (stop_result.is_err())
166 {
167 log_module::write_error("error stopping thread pool: {}",
168 stop_result.error().message);
169 }
170
171 log_module::write_information("stopped {}", thread_pool->to_string());
172
173 thread_pool.reset();
174
175 log_module::stop();
176
177 return 0;
178}
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 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.
auto store_job(std::shared_ptr< thread_pool > thread_pool) -> kcenon::common::VoidResult
auto initialize_logger() -> std::optional< std::string >
uint16_t thread_counts_
auto create_default(const uint16_t &worker_counts) -> std::tuple< std::shared_ptr< thread_pool >, kcenon::common::VoidResult >

References create_default(), kcenon::thread::utils::formatter::format(), initialize_logger(), kcenon::thread::thread_pool::start(), kcenon::thread::thread_pool::stop(), store_job(), thread_counts_, and kcenon::thread::thread_pool::to_string().

Here is the call graph for this function:

◆ store_job()

auto store_job ( std::shared_ptr< thread_pool > thread_pool) -> kcenon::common::VoidResult
Examples
thread_pool_sample.cpp, typed_thread_pool_sample.cpp, and typed_thread_pool_sample_2.cpp.

Definition at line 92 of file thread_pool_sample.cpp.

93{
94 std::vector<std::unique_ptr<job>> jobs;
95 jobs.reserve(test_line_count_);
96
97 for (auto index = 0; index < test_line_count_; ++index)
98 {
99 jobs.push_back(std::make_unique<callback_job>(
100 [index](void) -> kcenon::common::VoidResult
101 {
102 log_module::write_debug("Hello, World!: {}", index);
103 return kcenon::common::ok();
104 }));
105 }
106
107 auto result = thread_pool->enqueue_batch(std::move(jobs));
108 if (result.is_err())
109 {
110 return result.error();
111 }
112
113 log_module::write_sequence("enqueued jobs: {}", test_line_count_);
114
115 return kcenon::common::ok();
116}
A template class representing either a value or an error.
auto enqueue_batch(std::vector< std::unique_ptr< job > > &&jobs) -> common::VoidResult
Enqueues a batch of jobs into the shared job_queue.
uint32_t test_line_count_

References kcenon::thread::thread_pool::enqueue_batch(), and test_line_count_.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ callback_target_

log_module::log_types callback_target_ = log_module::log_types::None

Definition at line 34 of file thread_pool_sample.cpp.

Referenced by initialize_logger().

◆ console_target_

log_module::log_types console_target_ = log_module::log_types::Information

Definition at line 33 of file thread_pool_sample.cpp.

Referenced by initialize_logger().

◆ file_target_

log_module::log_types file_target_ = log_module::log_types::None

Definition at line 32 of file thread_pool_sample.cpp.

Referenced by initialize_logger().

◆ max_lines_

uint32_t max_lines_ = 0

Definition at line 29 of file thread_pool_sample.cpp.

Referenced by initialize_logger().

◆ test_line_count_

uint32_t test_line_count_ = 1000000

Definition at line 31 of file thread_pool_sample.cpp.

Referenced by store_job().

◆ thread_counts_

uint16_t thread_counts_ = 10
Examples
thread_pool_sample.cpp.

Definition at line 36 of file thread_pool_sample.cpp.

Referenced by main().

◆ use_backup_

bool use_backup_ = false

Definition at line 28 of file thread_pool_sample.cpp.

Referenced by initialize_logger().

◆ wait_interval_

uint16_t wait_interval_ = 100

Definition at line 30 of file thread_pool_sample.cpp.

Referenced by initialize_logger().