asynchrony 2.3.1
Add asynchrony to your C++ applications using standard C++20
Loading...
Searching...
No Matches
siddiqsoft::periodic_worker< Pri > Struct Template Reference

Public Member Functions

 periodic_worker (periodic_worker &)=delete
 Copy constructor (deleted - workers are not copyable).
auto & operator= (periodic_worker &)=delete
 Copy assignment operator (deleted - workers are not copyable).
 periodic_worker (periodic_worker &&)=delete
 Move constructor (deleted - workers are not movable).
auto & operator= (periodic_worker &&)=delete
 Move assignment operator (deleted - workers are not movable).
 ~periodic_worker ()
 Destructor - gracefully shuts down the periodic worker thread.
void forceCleanupTerminate (const std::source_location &sl=std::source_location::current())
 Force immediate termination of the worker thread.
 periodic_worker (std::function< void()> c, std::chrono::microseconds interval, std::string name={"anonymous-periodic-worker"})
 Constructs a periodic worker thread.

Static Public Attributes

static constexpr std::chrono::milliseconds DEFAULT_WAIT_FOR_NEXT_ITEM_MS {1500}
 Default wait interval for the worker thread.

Detailed Description

template<int Pri = 0>
requires ((Pri >= -10) && (Pri <= 10))
struct siddiqsoft::periodic_worker< Pri >
Examples
/opt/azure-agent/_work/18/s/include/siddiqsoft/periodic_worker.hpp.

Definition at line 102 of file periodic_worker.hpp.

Constructor & Destructor Documentation

◆ ~periodic_worker()

template<int Pri = 0>
siddiqsoft::periodic_worker< Pri >::~periodic_worker ( )
inline

Destructor - gracefully shuts down the periodic worker thread.

Performs the following cleanup steps:

  1. Sets the invoke period to 0 microseconds to wake up the waiting thread immediately
  2. Releases the semaphore to signal the thread
  3. Requests the worker thread to stop via stop_token
  4. Waits briefly for the thread to respond
  5. Allows jthread to join automatically
Remarks
This approach is critical because:
  • Without reducing the interval, the thread might wait for the full period before shutting down
  • Setting interval to 0 ensures immediate wakeup from the semaphore wait
  • Saves approximately 100ms+ of idle time compared to default timeout
Note
In debug builds, logs shutdown progress and statistics

Definition at line 138 of file periodic_worker.hpp.

◆ periodic_worker()

template<int Pri = 0>
siddiqsoft::periodic_worker< Pri >::periodic_worker ( std::function< void()> c,
std::chrono::microseconds interval,
std::string name = {"anonymous-periodic-worker"} )
inline

Constructs a periodic worker thread.

Creates a single worker thread that will invoke the callback at the specified interval.

Parameters
cThe worker callback function with signature void() Called periodically at the specified interval
intervalThe time interval between callback invocations
nameOptional name for the worker thread (useful for debugging)
  • The callback is stored and invoked by the worker thread
  • The worker thread starts immediately and waits for the first interval
  • Thread priority is set if Pri != 0 (Windows only)
  • Exceptions in callbacks are caught and logged to prevent thread termination
  • The interval can be modified at runtime via the invokePeriod atomic variable

Definition at line 244 of file periodic_worker.hpp.

Member Function Documentation

◆ forceCleanupTerminate()

template<int Pri = 0>
void siddiqsoft::periodic_worker< Pri >::forceCleanupTerminate ( const std::source_location & sl = std::source_location::current())
inline

Force immediate termination of the worker thread.

This method should only be used during application shutdown when the callback cannot be guaranteed to be "clean" or respect the stop_token. It forcefully terminates the thread using platform-specific APIs.

Parameters
slSource location for logging purposes (automatically captured)
Warning
This is a last-resort cleanup method and should only be called when normal shutdown has failed. Using this during normal operation can lead to resource leaks and undefined behavior.
  • On POSIX systems: calls pthread_cancel() and detaches the thread
  • On Windows: calls TerminateThread() and detaches the thread
  • Uses std::call_once to ensure this is only called once
  • Logs a warning message with the source location

Definition at line 194 of file periodic_worker.hpp.

Member Data Documentation

◆ DEFAULT_WAIT_FOR_NEXT_ITEM_MS

template<int Pri = 0>
std::chrono::milliseconds siddiqsoft::periodic_worker< Pri >::DEFAULT_WAIT_FOR_NEXT_ITEM_MS {1500}
staticconstexpr

Default wait interval for the worker thread.

Definition at line 105 of file periodic_worker.hpp.


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