Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
service_registry_sample/main.cpp

Demonstrates registering a simple service in the global service_registry, retrieving it, and running a basic thread pool with a single worker.

See also
service_registry, thread_pool, callback_job
// BSD 3-Clause License
// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
// See the LICENSE file in the project root for full license information.
#include <iostream>
#include <memory>
#include <thread>
#include <atomic>
using namespace kcenon::thread;
struct demo_service {
std::string name;
};
int main() {
// Register and get a simple service
auto svc = std::make_shared<demo_service>();
svc->name = "demo";
service_registry::register_service<demo_service>(svc);
auto got = service_registry::get_service<demo_service>();
std::cout << "service name = " << (got ? got->name : "<null>") << "\n";
// Use thread_pool directly (not via deprecated executor_interface)
auto pool = std::make_shared<thread_pool>("svc_pool");
std::vector<std::unique_ptr<thread_worker>> workers;
workers.push_back(std::make_unique<thread_worker>(false));
if (auto r = pool->enqueue_batch(std::move(workers)); r.is_err()) {
std::cerr << r.error().message << "\n";
return 1;
}
if (auto r = pool->start(); r.is_err()) {
std::cerr << r.error().message << "\n";
return 1;
}
std::atomic<int> count{0};
if (auto r = pool->enqueue(std::make_unique<callback_job>([&count]() -> kcenon::common::VoidResult {
count.fetch_add(1);
return kcenon::common::ok();
})); r.is_err()) {
std::cerr << r.error().message << "\n";
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
std::cout << "executed jobs = " << count.load() << "\n";
pool->stop();
return 0;
}
Specialized job class that encapsulates user-defined callbacks.
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.
std::string name
Definition main.cpp:29
Specialized worker thread that processes jobs from a job_queue.