Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
thread_integration.h
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
5#pragma once
6
18#include <functional>
19#include <memory>
20#include <future>
21#include <chrono>
22#include <vector>
23
25
33class thread_pool_interface {
34public:
35 virtual ~thread_pool_interface() = default;
36
42 virtual std::future<void> submit(std::function<void()> task) = 0;
43
50 virtual std::future<void> submit_delayed(
51 std::function<void()> task,
52 std::chrono::milliseconds delay
53 ) = 0;
54
59 virtual size_t worker_count() const = 0;
60
65 virtual bool is_running() const = 0;
66
71 virtual size_t pending_tasks() const = 0;
72};
73
82public:
87 explicit basic_thread_pool(size_t num_threads = 0);
88
90
91 // thread_pool_interface implementation
92 std::future<void> submit(std::function<void()> task) override;
93 std::future<void> submit_delayed(
94 std::function<void()> task,
95 std::chrono::milliseconds delay
96 ) override;
97 size_t worker_count() const override;
98 bool is_running() const override;
99 size_t pending_tasks() const override;
100
105 void stop(bool wait_for_tasks = true);
106
111 size_t completed_tasks() const;
112
113private:
114 class impl;
115
134 std::shared_ptr<impl> pimpl_;
135};
136
145public:
151
156 void set_thread_pool(std::shared_ptr<thread_pool_interface> pool);
157
162 std::shared_ptr<thread_pool_interface> get_thread_pool();
163
169 std::future<void> submit_task(std::function<void()> task);
170
177 std::future<void> submit_delayed_task(
178 std::function<void()> task,
179 std::chrono::milliseconds delay
180 );
181
185 struct metrics {
186 size_t worker_threads = 0;
187 size_t pending_tasks = 0;
188 size_t completed_tasks = 0;
189 bool is_running = false;
190 };
191
196 metrics get_metrics() const;
197
198private:
201
202 class impl;
203
222 std::shared_ptr<impl> pimpl_;
223};
224
225} // namespace kcenon::network::integration
226
227// Backward compatibility
228namespace network_system {
230}
Basic thread pool implementation for standalone use.
size_t worker_count() const override
Get the number of worker threads.
void stop(bool wait_for_tasks=true)
Stop the thread pool.
bool is_running() const override
Check if the thread pool is running.
size_t completed_tasks() const
Get completed tasks count.
basic_thread_pool(size_t num_threads=0)
Construct with specified number of threads.
std::shared_ptr< impl > pimpl_
PIMPL pointer with intentional leak pattern.
std::future< void > submit_delayed(std::function< void()> task, std::chrono::milliseconds delay) override
Submit a task with delay.
size_t pending_tasks() const override
Get pending task count.
std::future< void > submit(std::function< void()> task) override
Submit a task to the thread pool.
void set_thread_pool(std::shared_ptr< thread_pool_interface > pool)
Set the thread pool implementation.
std::future< void > submit_delayed_task(std::function< void()> task, std::chrono::milliseconds delay)
Submit a task with delay.
static thread_integration_manager & instance()
Get the singleton instance.
std::shared_ptr< impl > pimpl_
PIMPL pointer with intentional leak pattern.
std::future< void > submit_task(std::function< void()> task)
Submit a task to the thread pool.
std::shared_ptr< thread_pool_interface > get_thread_pool()
Get the current thread pool.
Interface for thread pool integration.
Definition core.cppm:83
virtual size_t worker_count() const =0
Get the number of worker threads.
virtual std::future< void > submit(std::function< void()> task)=0
Submit a task to the thread pool.
virtual std::future< void > submit_delayed(std::function< void()> task, std::chrono::milliseconds delay)=0
Submit a task with delay.
virtual size_t pending_tasks() const =0
Get pending task count.
virtual bool is_running() const =0
Check if the thread pool is running.