asynchrony 2.3.1
Add asynchrony to your C++ applications using standard C++20
Loading...
Searching...
No Matches
siddiqsoft::simple_worker< T, Pri > Struct Template Reference
Collaboration diagram for siddiqsoft::simple_worker< T, Pri >:

Public Member Functions

 simple_worker (const simple_worker &)=delete
 Copy constructor (deleted - workers are not copyable).
simple_workeroperator= (const simple_worker &)=delete
 Copy assignment operator (deleted - workers are not copyable).
 ~simple_worker ()
 Destructor - gracefully shuts down the worker thread.
bool shutdown (std::chrono::milliseconds timeout=DEFAULT_SHUTDOWN_DRAIN_MS)
 simple_worker (simple_worker &&)=delete
 Move constructor (deleted - workers are not movable).
simple_workeroperator= (simple_worker &&)=delete
 Move assignment operator (deleted - workers are not movable).
 simple_worker (std::function< void(T &&)> c)
 Constructs a worker thread with the given callback.
void queue (T &&item) noexcept(false)
 Queue a work item for processing.

Public Attributes

std::atomic< bool > accepting_items {true}
std::atomic< bool > shutdown_initiated {false}
std::once_flag shutdown_invoked

Static Public Attributes

static constexpr std::chrono::milliseconds DEFAULT_WAIT_FOR_NEXT_ITEM_MS {1500}
 Default wait interval for the worker thread waiting on items.
static constexpr std::chrono::milliseconds DEFAULT_SHUTDOWN_DRAIN_MS {1000}

Detailed Description

template<typename T, int Pri = 0>
requires ((Pri >= -10) && (Pri <= 10)) && std::move_constructible<T>
struct siddiqsoft::simple_worker< T, Pri >
Examples
/opt/azure-agent/_work/18/s/include/siddiqsoft/simple_worker.hpp.

Definition at line 105 of file simple_worker.hpp.

Constructor & Destructor Documentation

◆ ~simple_worker()

template<typename T, int Pri = 0>
siddiqsoft::simple_worker< T, Pri >::~simple_worker ( )
inline

Destructor - gracefully shuts down the worker thread.

Performs the following cleanup steps:

  1. Waits for the queue to be empty (all pending items processed)
  2. Requests the worker thread to stop via stop_token
  3. Joins the thread to ensure clean shutdown
Remarks
In debug builds, logs the queue state before shutdown
Examples
/opt/azure-agent/_work/18/s/include/siddiqsoft/simple_worker.hpp.

Definition at line 133 of file simple_worker.hpp.

◆ simple_worker()

template<typename T, int Pri = 0>
siddiqsoft::simple_worker< T, Pri >::simple_worker ( std::function< void(T &&)> c)
inline

Constructs a worker thread with the given callback.

Creates a single worker thread that will process items from the queue using the provided callback function.

Parameters
cThe worker callback function with signature void(T&&) Called for each item dequeued from the worker's queue
  • The callback is stored and invoked by the worker thread
  • The worker thread starts immediately and waits for items
  • Thread priority is set if Pri != 0 (Windows only)
  • Exceptions in callbacks are caught and logged to prevent thread termination

Definition at line 212 of file simple_worker.hpp.

Member Function Documentation

◆ queue()

template<typename T, int Pri = 0>
void siddiqsoft::simple_worker< T, Pri >::queue ( T && item)
inline

Queue a work item for processing.

Adds an item to the worker's queue for asynchronous processing. The worker thread will process this item as soon as it becomes available.

Parameters
itemThe work item to queue (must be move-constructible) Ownership is transferred to the worker
  • Thread-safe operation
  • Increments the queue counter with release semantics
  • Item is moved into the internal queue
  • Worker thread is signaled to wake up if waiting
Note
The item is moved into the queue, so the original is no longer valid
Examples
/opt/azure-agent/_work/18/s/include/siddiqsoft/simple_worker.hpp.

Definition at line 235 of file simple_worker.hpp.

◆ shutdown()

template<typename T, int Pri = 0>
bool siddiqsoft::simple_worker< T, Pri >::shutdown ( std::chrono::milliseconds timeout = DEFAULT_SHUTDOWN_DRAIN_MS)
inline

Definition at line 142 of file simple_worker.hpp.

Member Data Documentation

◆ accepting_items

template<typename T, int Pri = 0>
std::atomic<bool> siddiqsoft::simple_worker< T, Pri >::accepting_items {true}

Definition at line 107 of file simple_worker.hpp.

◆ DEFAULT_SHUTDOWN_DRAIN_MS

template<typename T, int Pri = 0>
std::chrono::milliseconds siddiqsoft::simple_worker< T, Pri >::DEFAULT_SHUTDOWN_DRAIN_MS {1000}
staticconstexpr

Definition at line 113 of file simple_worker.hpp.

◆ DEFAULT_WAIT_FOR_NEXT_ITEM_MS

template<typename T, int Pri = 0>
std::chrono::milliseconds siddiqsoft::simple_worker< T, Pri >::DEFAULT_WAIT_FOR_NEXT_ITEM_MS {1500}
staticconstexpr

Default wait interval for the worker thread waiting on items.

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

Definition at line 112 of file simple_worker.hpp.

◆ shutdown_initiated

template<typename T, int Pri = 0>
std::atomic<bool> siddiqsoft::simple_worker< T, Pri >::shutdown_initiated {false}

Definition at line 108 of file simple_worker.hpp.

◆ shutdown_invoked

template<typename T, int Pri = 0>
std::once_flag siddiqsoft::simple_worker< T, Pri >::shutdown_invoked

Definition at line 109 of file simple_worker.hpp.


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