Class Template siddiqsoft::simple_worker¶
Header: #include "siddiqsoft/simple_worker.hpp"
template <typename T, int ThreadPriority = 0>
requires((ThreadPriority >= -10) && (ThreadPriority <= 10)) && std::move_constructible<T>
struct simple_worker;
A single-threaded asynchronous worker that queues tasks of type T and processes them sequentially in a dedicated background std::jthread.
Template Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
T |
typename |
Required | Work item type. Must satisfy std::move_constructible. |
ThreadPriority |
int |
0 |
Thread priority level (-10 to +10). On Windows, passed to SetThreadPriority(). |
Static Constants¶
| Constant | Type | Value | Description |
|---|---|---|---|
DEFAULT_WAIT_FOR_NEXT_ITEM_MS |
std::chrono::milliseconds |
1500 |
Default interval worker thread waits on internal queue before re-checking stop token. |
DEFAULT_SHUTDOWN_DRAIN_MS |
std::chrono::milliseconds |
1000 |
Default timeout duration for queue draining during shutdown. |
Public Data Members¶
| Member | Type | Default | Description |
|---|---|---|---|
accepting_items |
std::atomic<bool> |
true |
Indicates whether new items can be queued. Set to false when shutdown() begins. |
shutdown_initiated |
std::atomic<bool> |
false |
Indicates whether worker shutdown sequence has been started. |
shutdown_invoked |
std::once_flag |
— | std::once_flag ensuring shutdown() logic executes exactly once. |
Member Functions¶
Constructor¶
Constructs a simple_worker and immediately launches the background processing thread.
- Parameters:
callback– Function or callable invoked for each dequeued item. - Copy/Move: Copy and move constructors are deleted.
Example¶
#include "siddiqsoft/simple_worker.hpp"
#include <iostream>
#include <string>
// Create a simple worker that processes strings
siddiqsoft::simple_worker<std::string> worker([](std::string&& item) {
std::cout << "Processing item: " << item << std::endl;
});
Destructor¶
Gracefully shuts down the worker thread. Automatically invokes shutdown() to drain remaining tasks and join the background thread before object destruction completes.
Example¶
{
siddiqsoft::simple_worker<std::string> worker([](std::string&& item) {
std::println(item);
});
worker.queue("Task A");
worker.queue("Task B");
} // Destructor runs here: drains pending items and joins background thread cleanly.
queue¶
Pushes an item into the worker's internal queue. Ownership of item is transferred to the worker via move semantics.
- Parameters:
item– Rvalue reference to work item. - Exceptions: Throws
std::runtime_errorif the worker is currently shutting down (accepting_itemsisfalse). - Thread Safety: Safe to call concurrently from multiple producer threads.
Example¶
siddiqsoft::simple_worker<std::string> worker([](std::string&& item) {
std::cout << "Handled: " << item << std::endl;
});
// Queue using temporary rvalue
worker.queue("Instant message");
// Queue using std::move
std::string msg = "Deferred payload";
worker.queue(std::move(msg));
shutdown¶
Initiates a graceful shutdown of the worker thread.
- Stops accepting new items (
accepting_itemsset tofalse). - Waits up to
timeoutduration for the internal queue to drain. - Signals the background thread to stop and joins it.
-
Uses
std::call_onceinternally so subsequent calls return the cached shutdown status. -
Parameters:
timeout– Maximum duration to wait for the queue to drain (default1000ms). - Returns:
bool–trueif all items were drained before timeout;falseif timeout occurred.
Example¶
siddiqsoft::simple_worker<int> worker([](int&& val) {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
});
for (int i = 0; i < 10; ++i) {
worker.queue(i + 1);
}
// Gracefully shutdown with a custom 2-second timeout
bool drained = worker.shutdown(std::chrono::seconds(2));
if (drained) {
std::println("All items processed successfully before shutdown.");
}
to_json¶
Generates a JSON snapshot containing worker diagnostics and telemetry. Available when NLOHMANN_JSON_VERSION_MAJOR is defined.
- Returns:
nlohmann::jsonobject containing:_typver: Type version string ("siddiqsoft.asynchrony.simple_worker/2.3.3")itemsSize: Current number of items in queuequeueCounter: Total items queued counteritemsQueued: Total items addeditemsPopped: Total items processeditemsOutstanding: Number of items currently queued but not processedthreadPriority: Priority integer settingoutstandingCallback: Active executing callback countwaitInterval: Wait interval in milliseconds
Also provides a free function overload in siddiqsoft namespace:
template <typename T, int Pri = 0>
static void to_json(nlohmann::json& dest, const siddiqsoft::simple_worker<T, Pri>& src);