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
#include <iostream>
#include <memory>
#include <thread>
#include <atomic>
};
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";
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.
Lightweight service registry for dependency lookup within the thread system.
Specialized worker thread that processes jobs from a job_queue.