Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
16#include <iostream>
17#include <memory>
18#include <thread>
19#include <atomic>
20
25
26using namespace kcenon::thread;
27
29 std::string name;
30};
31
32int main() {
33 // Register and get a simple service
34 auto svc = std::make_shared<demo_service>();
35 svc->name = "demo";
38 std::cout << "service name = " << (got ? got->name : "<null>") << "\n";
39
40 // Use thread_pool directly (not via deprecated executor_interface)
41 auto pool = std::make_shared<thread_pool>("svc_pool");
42 std::vector<std::unique_ptr<thread_worker>> workers;
43 workers.push_back(std::make_unique<thread_worker>(false));
44 if (auto r = pool->enqueue_batch(std::move(workers)); r.is_err()) {
45 std::cerr << r.error().message << "\n";
46 return 1;
47 }
48 if (auto r = pool->start(); r.is_err()) {
49 std::cerr << r.error().message << "\n";
50 return 1;
51 }
52
53 std::atomic<int> count{0};
54 if (auto r = pool->enqueue(std::make_unique<callback_job>([&count]() -> kcenon::common::VoidResult {
55 count.fetch_add(1);
56 return kcenon::common::ok();
57 })); r.is_err()) {
58 std::cerr << r.error().message << "\n";
59 }
60
61 std::this_thread::sleep_for(std::chrono::milliseconds(50));
62 std::cout << "executed jobs = " << count.load() << "\n";
63 pool->stop();
64 return 0;
65}
Specialized job class that encapsulates user-defined callbacks.
static auto get_service() -> std::shared_ptr< Interface >
static auto register_service(std::shared_ptr< Interface > service) -> void
Register a service implementation for the given interface type.
Core thread pool implementation with work stealing and auto-scaling.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
Lightweight service registry for dependency lookup within the thread system.
int main()
Definition main.cpp:32
std::string name
Definition main.cpp:29
Specialized worker thread that processes jobs from a job_queue.