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
133 std::shared_ptr<impl> pimpl_;
134};
135
144public:
150
155 void set_thread_pool(std::shared_ptr<thread_pool_interface> pool);
156
161 std::shared_ptr<thread_pool_interface> get_thread_pool();
162
168 std::future<void> submit_task(std::function<void()> task);
169
176 std::future<void> submit_delayed_task(
177 std::function<void()> task,
178 std::chrono::milliseconds delay
179 );
180
184 struct metrics {
185 size_t worker_threads = 0;
186 size_t pending_tasks = 0;
187 size_t completed_tasks = 0;
188 bool is_running = false;
189 };
190
195 metrics get_metrics() const;
196
197private:
200
201 class impl;
202
220 std::shared_ptr<impl> pimpl_;
221};
222
223} // namespace kcenon::network::integration
224
225// Backward compatibility
226namespace network_system {
228}
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.