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

Service registry sample showing singleton service registration and thread pool usage. More...

#include <iostream>
#include <memory>
#include <thread>
#include <atomic>
#include <kcenon/thread/core/service_registry.h>
#include <kcenon/thread/core/thread_pool.h>
#include <kcenon/thread/core/thread_worker.h>
#include <kcenon/thread/core/callback_job.h>
Include dependency graph for main.cpp:

Go to the source code of this file.

Classes

struct  demo_service
 

Functions

int main ()
 

Detailed Description

Service registry sample showing singleton service registration and thread pool usage.

Definition in file main.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 32 of file main.cpp.

32 {
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}
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.

References kcenon::thread::service_registry::get_service(), and kcenon::thread::service_registry::register_service().

Here is the call graph for this function: