Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
minimal_thread_pool.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
17#include <atomic>
18#include <chrono>
19#include <iostream>
20#include <thread>
21
26
27using namespace kcenon::thread;
28using namespace kcenon::thread;
29
30int main() {
31 std::cout << "=== Minimal Thread Pool Sample (No Logger) ===" << std::endl;
32
33 // Create thread pool
34 thread_context context;
35 auto pool = std::make_shared<thread_pool>("MinimalPool", context);
36
37 // Create workers
38 const size_t worker_count = 4;
39 std::vector<std::unique_ptr<thread_worker>> workers;
40
41 for (size_t i = 0; i < worker_count; ++i) {
42 workers.push_back(std::make_unique<thread_worker>(false, context));
43 }
44
45 // Add workers to pool
46 auto result = pool->enqueue_batch(std::move(workers));
47 if (result.is_err()) {
48 std::cerr << "Error adding workers: " << result.error().message << std::endl;
49 return 1;
50 }
51
52 // Start the pool
53 result = pool->start();
54 if (result.is_err()) {
55 std::cerr << "Error starting pool: " << result.error().message << std::endl;
56 return 1;
57 }
58
59 std::cout << "Thread pool started with " << worker_count << " workers" << std::endl;
60
61 // Submit some jobs
62 std::atomic<int> completed_jobs{0};
63 const int total_jobs = 20;
64
65 std::cout << "Submitting " << total_jobs << " jobs..." << std::endl;
66
67 for (int i = 0; i < total_jobs; ++i) {
68 auto job = std::make_unique<callback_job>(
69 [i, &completed_jobs, total_jobs]() -> kcenon::common::VoidResult {
70 // Simulate some work
71 std::this_thread::sleep_for(std::chrono::milliseconds(100));
72
73 // Print progress (thread-safe)
74 int current = completed_jobs.fetch_add(1) + 1;
75 std::cout << "Job " << i << " completed. Total: "
76 << current << "/" << total_jobs << std::endl;
77
78 return kcenon::common::ok();
79 },
80 "job_" + std::to_string(i)
81 );
82
83 result = pool->enqueue(std::move(job));
84 if (result.is_err()) {
85 std::cerr << "Error enqueuing job: " << result.error().message << std::endl;
86 }
87 }
88
89 // Wait for all jobs to complete
90 std::cout << "Waiting for jobs to complete..." << std::endl;
91 while (completed_jobs.load() < total_jobs) {
92 std::this_thread::sleep_for(std::chrono::milliseconds(100));
93 }
94
95 std::cout << "All jobs completed!" << std::endl;
96
97 // Stop the pool
98 auto stop_result = pool->stop();
99 if (stop_result.is_err()) {
100 std::cerr << "Error stopping pool: " << stop_result.error().message << std::endl;
101 }
102 std::cout << "Thread pool stopped." << std::endl;
103
104 return 0;
105}
Specialized job class that encapsulates user-defined callbacks.
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136
A template class representing either a value or an error.
Context object that provides access to optional services.
Core thread pool implementation with work stealing and auto-scaling.
int main()
Core threading foundation of the thread system library.
Definition thread_impl.h:17
Context object providing access to optional thread system services.
Specialized worker thread that processes jobs from a job_queue.