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

Composition-based design with service container, logger, and monitoring. More...

#include <iostream>
#include <chrono>
#include <thread>
#include <kcenon/thread/interfaces/service_container.h>
#include <kcenon/thread/interfaces/thread_context.h>
#include <kcenon/common/interfaces/logger_interface.h>
#include <kcenon/common/interfaces/global_logger_registry.h>
#include <kcenon/common/interfaces/monitoring_interface.h>
#include <kcenon/thread/core/thread_pool.h>
#include <kcenon/thread/core/callback_job.h>
#include <kcenon/thread/core/log_level.h>
Include dependency graph for composition_example.cpp:

Go to the source code of this file.

Classes

class  console_logger
 Simple console logger implementation using common_system ILogger. More...
 
class  console_monitoring
 Simple monitoring implementation using common::interfaces::IMonitor. More...
 

Typedefs

using common_log_level = kcenon::common::interfaces::log_level
 

Functions

void demonstrate_composition ()
 Demonstrate composition-based design.
 
void demonstrate_minimal_usage ()
 Demonstrate using thread pool without any services.
 
int main ()
 

Detailed Description

Composition-based design with service container, logger, and monitoring.

Definition in file composition_example.cpp.

Typedef Documentation

◆ common_log_level

using common_log_level = kcenon::common::interfaces::log_level

Definition at line 33 of file composition_example.cpp.

Function Documentation

◆ demonstrate_composition()

void demonstrate_composition ( )

Demonstrate composition-based design.

Examples
composition_example.cpp.

Definition at line 154 of file composition_example.cpp.

154 {
155 std::cout << "\n=== Composition-Based Thread System Demo ===\n" << std::endl;
156
157 // 1. Setup service container with implementations
158 auto& container = service_container::global();
159
160 // Register logger service
161 container.register_singleton<kcenon::common::interfaces::ILogger>(
162 std::make_shared<console_logger>());
163
164 // Register monitoring service
165 container.register_singleton<kcenon::common::interfaces::IMonitor>(
166 std::make_shared<console_monitoring>());
167
168 // 2. Create thread pool with context from global container
169 thread_context context; // Will resolve services from container
170 auto pool = std::make_shared<thread_pool>("CompositionPool", context);
171
172 // 3. Add workers - they inherit context from pool
173 std::vector<std::unique_ptr<thread_worker>> workers;
174 for (int i = 0; i < 4; ++i) {
175 workers.push_back(std::make_unique<thread_worker>());
176 }
177 {
178 auto r = pool->enqueue_batch(std::move(workers));
179 if (r.is_err()) {
180 std::cerr << "enqueue_batch failed: " << r.error().message << std::endl;
181 return;
182 }
183 }
184
185 // 4. Start pool - will log through context
186 {
187 auto r = pool->start();
188 if (r.is_err()) {
189 std::cerr << "start failed: " << r.error().message << std::endl;
190 return;
191 }
192 }
193
194 // 5. Submit jobs that will be logged
195 for (int i = 0; i < 10; ++i) {
196 auto r = pool->enqueue(std::make_unique<callback_job>(
197 [i, &context]() -> kcenon::common::VoidResult {
198 context.log(log_level_v2::info,
199 "Processing job " + std::to_string(i));
200
201 // Simulate work
202 std::this_thread::sleep_for(std::chrono::milliseconds(100));
203
204 return kcenon::common::ok();
205 }
206 ));
207 if (r.is_err()) {
208 std::cerr << "enqueue failed: " << r.error().message << std::endl;
209 }
210 }
211
212 // 6. Wait for completion
213 std::this_thread::sleep_for(std::chrono::seconds(2));
214
215 // 7. Stop pool
216 {
217 auto r = pool->stop();
218 if (r.is_err()) {
219 std::cerr << "stop failed: " << r.error().message << std::endl;
220 }
221 }
222
223 std::cout << "\n=== Basic Thread Pool Demo Complete ===\n" << std::endl;
224}
static service_container & global()
Get the global service container instance.
Context object that provides access to optional services.

References kcenon::thread::service_container::global().

Referenced by main().

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

◆ demonstrate_minimal_usage()

void demonstrate_minimal_usage ( )

Demonstrate using thread pool without any services.

Examples
composition_example.cpp.

Definition at line 306 of file composition_example.cpp.

306 {
307 std::cout << "\n=== Minimal Thread Pool (No Services) Demo ===\n" << std::endl;
308
309 // Clear any existing services
311
312 // Create pool without context - no logging or monitoring
313 auto pool = std::make_shared<thread_pool>("MinimalPool");
314
315 // Add workers
316 std::vector<std::unique_ptr<thread_worker>> workers;
317 for (int i = 0; i < 2; ++i) {
318 workers.push_back(std::make_unique<thread_worker>());
319 }
320 {
321 auto r = pool->enqueue_batch(std::move(workers));
322 if (r.is_err()) {
323 std::cerr << "enqueue_batch failed: " << r.error().message << std::endl;
324 return;
325 }
326 }
327
328 {
329 auto r = pool->start();
330 if (r.is_err()) {
331 std::cerr << "start failed: " << r.error().message << std::endl;
332 return;
333 }
334 }
335
336 // Submit jobs - no logging will occur
337 std::atomic<int> counter{0};
338 for (int i = 0; i < 5; ++i) {
339 auto r = pool->enqueue(std::make_unique<callback_job>(
340 [&counter]() -> kcenon::common::VoidResult {
341 counter.fetch_add(1);
342 return kcenon::common::ok();
343 }
344 ));
345 if (r.is_err()) {
346 std::cerr << "enqueue failed: " << r.error().message << std::endl;
347 }
348 }
349
350 std::this_thread::sleep_for(std::chrono::milliseconds(500));
351 {
352 auto r = pool->stop();
353 if (r.is_err()) {
354 std::cerr << "stop failed: " << r.error().message << std::endl;
355 }
356 }
357
358 std::cout << "Completed " << counter.load() << " jobs without any logging/monitoring" << std::endl;
359 std::cout << "\n=== Minimal Demo Complete ===\n" << std::endl;
360}
void clear()
Clear all registered services.

References kcenon::thread::service_container::clear(), and kcenon::thread::service_container::global().

Referenced by main().

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

◆ main()

int main ( )

Definition at line 362 of file composition_example.cpp.

362 {
363 try {
364 // Show different usage patterns
367 // demonstrate_typed_pool_composition(); // Temporarily disabled - requires typed_pool implementation
368
369 // Clean up
371
372 std::cout << "\nAll demos completed successfully!" << std::endl;
373
374 } catch (const std::exception& e) {
375 std::cerr << "Error: " << e.what() << std::endl;
376 return 1;
377 }
378
379 return 0;
380}
void demonstrate_composition()
Demonstrate composition-based design.
void demonstrate_minimal_usage()
Demonstrate using thread pool without any services.

References kcenon::thread::service_container::clear(), demonstrate_composition(), demonstrate_minimal_usage(), and kcenon::thread::service_container::global().

Here is the call graph for this function: