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

Public Member Functions

 simple_pool (simple_pool &&)=delete
 Move constructor (deleted - pools are not movable).
simple_pooloperator= (simple_pool &&)=delete
 Move assignment operator (deleted - pools are not movable).
 simple_pool (simple_pool &)=delete
 Copy constructor (deleted - pools are not copyable).
simple_pooloperator= (simple_pool &)=delete
 Copy assignment operator (deleted - pools are not copyable).
 ~simple_pool ()
 Destructor - gracefully shuts down all worker threads.
 simple_pool (std::function< void(T &&)> c)
 Constructs a thread pool with N worker threads.
void queue (T &&item)
 Queue a work item for processing.

Static Public Attributes

static constexpr std::chrono::milliseconds DEFAULT_WAIT_FOR_NEXT_ITEM_MS {1500}
 Default wait interval for threads waiting on the semaphore.

Detailed Description

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

Definition at line 91 of file simple_pool.hpp.

Constructor & Destructor Documentation

◆ ~simple_pool()

template<typename T, uint16_t N = 0>
siddiqsoft::simple_pool< T, N >::~simple_pool ( )
inline

Destructor - gracefully shuts down all worker threads.

Performs the following cleanup steps:

  1. Releases the semaphore to wake up waiting threads
  2. Requests all threads to stop via stop_token
  3. Joins all threads to ensure clean shutdown
Remarks
This saves approximately 100ms of idle time compared to letting threads timeout naturally on the default 1500ms wait interval
Examples
/opt/azure-agent/_work/18/s/include/siddiqsoft/simple_pool.hpp.

Definition at line 120 of file simple_pool.hpp.

◆ simple_pool()

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

Constructs a thread pool with N worker threads.

Creates a pool of worker threads that process items from a shared queue. Each thread runs a loop that waits for items and invokes the callback.

Parameters
cThe worker callback function with signature void(T&&) Called for each item dequeued from the pool
  • Reserves space for all threads upfront to avoid relocations
  • Creates N threads (or hardware_concurrency() if N is 0)
  • Each thread waits on the semaphore with a 1500ms timeout
  • Exceptions in callbacks are caught and logged to stderr
  • Threads continue running until stop_requested() returns true

Definition at line 152 of file simple_pool.hpp.

Member Function Documentation

◆ queue()

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

Queue a work item for processing.

Adds an item to the shared queue and signals a waiting thread to process it. This method is thread-safe and can be called from multiple threads concurrently.

Parameters
itemThe work item to queue (must be move-constructible) Ownership is transferred to the pool
  • Uses perfect forwarding to minimize copies
  • Increments the queue counter with release semantics for visibility
  • Releases the semaphore to wake up a waiting thread
  • Thread-safe for concurrent calls from multiple producers
Note
The item is moved into the internal queue, so the original is no longer valid
Examples
/opt/azure-agent/_work/18/s/include/siddiqsoft/simple_pool.hpp.

Definition at line 202 of file simple_pool.hpp.

Member Data Documentation

◆ DEFAULT_WAIT_FOR_NEXT_ITEM_MS

template<typename T, uint16_t N = 0>
std::chrono::milliseconds siddiqsoft::simple_pool< T, N >::DEFAULT_WAIT_FOR_NEXT_ITEM_MS {1500}
staticconstexpr

Default wait interval for threads waiting on the semaphore.

Examples
/opt/azure-agent/_work/18/s/include/siddiqsoft/simple_pool.hpp.

Definition at line 94 of file simple_pool.hpp.


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