asynchrony 2.3.1
Add asynchrony to your C++ applications using standard C++20
Loading...
Searching...
No Matches
siddiqsoft::roundrobin_pool< T, N > Struct Template Reference

Public Member Functions

 roundrobin_pool (roundrobin_pool &&)=delete
 Move constructor (deleted - pools are not movable).
auto operator= (roundrobin_pool &&)=delete
 Move assignment operator (deleted - pools are not movable).
 roundrobin_pool (roundrobin_pool &)=delete
 Copy constructor (deleted - pools are not copyable).
auto operator= (roundrobin_pool &)=delete
 Copy assignment operator (deleted - pools are not copyable).
 roundrobin_pool (std::function< void(T &&)> c)
 Constructs a round-robin thread pool.
void queue (T &&item)
 Queue a work item for processing.

Detailed Description

template<typename T, uint16_t N = 0>
requires std::is_move_constructible_v<T>
struct siddiqsoft::roundrobin_pool< T, N >
Examples
/opt/azure-agent/_work/18/s/include/siddiqsoft/roundrobin_pool.hpp.

Definition at line 90 of file roundrobin_pool.hpp.

Constructor & Destructor Documentation

◆ roundrobin_pool()

template<typename T, uint16_t N = 0>
siddiqsoft::roundrobin_pool< T, N >::roundrobin_pool ( std::function< void(T &&)> c)
inline

Constructs a round-robin thread pool.

Creates a deque of simple_worker<T> instances that will process work items using the provided callback function. Work items are distributed across workers using a round-robin algorithm.

Parameters
cThe worker callback function with signature void(T&&) Called for each item dequeued from a worker's queue
  • Creates N worker threads (or hardware_concurrency() if N is 0)
  • Uses std::deque to store workers (no relocation on growth)
  • Caches the worker count for efficient round-robin calculation
  • Each worker is initialized with the same callback
  • Workers start immediately and wait for items

Definition at line 123 of file roundrobin_pool.hpp.

Member Function Documentation

◆ queue()

template<typename T, uint16_t N = 0>
void siddiqsoft::roundrobin_pool< T, N >::queue ( T && item)
inline

Queue a work item for processing.

Adds an item to one of the worker's queues using round-robin distribution. The choice of worker is determined by the queue counter modulo the number of workers.

Parameters
itemThe work item to queue (must be move-constructible) Ownership is transferred to the selected worker
  • Uses atomic fetch_add to get a unique index for each queued item
  • Calculates worker index as (counter % workersSize)
  • Distributes items evenly across all workers
  • Thread-safe for concurrent calls from multiple producers
  • Each worker only pops from its own queue (single consumer per queue)
Remarks
The round-robin approach ensures:
  • Balanced load distribution across workers
  • No lock contention on the shared queue (each worker has its own)
  • Cost of modulo is cheaper than lock acquisition
  • If one worker is blocked, others continue processing
Note
The item is moved into the selected worker's queue
Examples
/opt/azure-agent/_work/18/s/include/siddiqsoft/roundrobin_pool.hpp.

Definition at line 158 of file roundrobin_pool.hpp.


The documentation for this struct was generated from the following file: