36#ifndef ROUNDROBIN_POOL_HPP
37#define ROUNDROBIN_POOL_HPP
41#include "simple_worker.hpp"
88 template <
typename T, u
int16_t N = 0>
89 requires std::is_move_constructible_v<T>
126 workersSize = (N > 0) ? N : std::thread::hardware_concurrency();
129 for (
unsigned i = 0; i < workersSize; i++) {
130 workers.emplace_back(c);
163 size_t idx = nextWorkerIndex();
164 workers.at(idx).queue(std::move(item));
166 queueCounter.fetch_add(1, std::memory_order_release);
169#if defined(NLOHMANN_JSON_VERSION_MAJOR)
185 nlohmann::json toJson()
const
187 return {{
"_typver",
"siddiqsoft.asynchrony-lib.roundrobin_pool/0.10"},
188 {
"workersSize", workersSize},
189 {
"queueCounter", queueCounter.load(std::memory_order_acquire)}};
196 std::atomic_uint64_t queueCounter {0};
200 std::atomic_uint64_t queueCounter {0};
213 std::deque<simple_worker<T>> workers {};
216 uint64_t workersSize {};
235 size_t nextWorkerIndex()
237 if (workersSize == 0)
return 0;
238 return static_cast<size_t>(queueCounter.load(std::memory_order_acquire) % workersSize);
242#if defined(NLOHMANN_JSON_VERSION_MAJOR)
253 template <
typename T, u
int16_t N = 0>
254 static void to_json(nlohmann::json& dest,
const siddiqsoft::roundrobin_pool<T, N>& src)
roundrobin_pool(std::function< void(T &&)> c)
Constructs a round-robin thread pool.
roundrobin_pool(roundrobin_pool &)=delete
Copy constructor (deleted - pools are not copyable).
void queue(T &&item)
Queue a work item for processing.
auto operator=(roundrobin_pool &&)=delete
Move assignment operator (deleted - pools are not movable).
auto operator=(roundrobin_pool &)=delete
Copy assignment operator (deleted - pools are not copyable).
roundrobin_pool(roundrobin_pool &&)=delete
Move constructor (deleted - pools are not movable).