Introduction
The asynchrony library provides a set of modern C++20 utilities for building asynchronous and multi-threaded applications. It leverages standard library features like std::jthread, std::semaphore, std::deque, and std::concepts to provide clean, efficient abstractions for common asynchronous patterns.
Key Features
- Single-threaded Worker: Process items asynchronously in a dedicated thread
- Thread Pool: Distribute work across multiple threads with a shared queue
- Round-Robin Pool: Minimize contention with per-thread queues
- Periodic Worker: Execute functions at regular intervals
- Resource Pool: Manage a pool of reusable resources
- Modern C++20: Uses only standard library features (no external dependencies for core functionality)
- Type-Safe: Leverages C++ concepts for compile-time type checking
- Exception Safe: Handles exceptions gracefully without thread termination
Requirements
- C++20 Support: Requires std::jthread and std::stop_token
- Compiler Support:
- GCC 10+
- MSVC 16.11+ (Visual Studio 2019 or later)
- Clang 10+ (with -fexperimental-library flag)
- Platform Support: Windows, Linux, macOS
Main Components
Quick Start
Simple Worker Example
#include "siddiqsoft/simple_worker.hpp"
struct MyTask {
std::string data;
void operator()() { }
};
int main() {
task();
}};
for (int i = 0; i < 100; ++i) {
worker.
queue(MyTask{
"data-" + std::to_string(i)});
}
std::this_thread::sleep_for(std::chrono::seconds(1));
return 0;
}
Implements a simple queue + semaphore driven asynchronous processor.
void queue(T &&item)
Queue item into this worker thread's deque.
Thread Pool Example
#include "siddiqsoft/simple_pool.hpp"
int main() {
task();
}};
for (int i = 0; i < 1000; ++i) {
pool.
queue(MyTask{
"data-" + std::to_string(i)});
}
std::this_thread::sleep_for(std::chrono::seconds(2));
return 0;
}
Implements a single deque based vector of jthreads. All threads wait on the next available item via s...
void queue(T &&item)
Queue item into the deque (takes "ownership" of the item).
Design Principles
- Move Semantics: All components use move semantics for efficient resource transfer
- RAII: Proper resource management through constructors and destructors
- Exception Safety: Exceptions in callbacks are caught and logged, not propagated
- Thread Safety: Internal synchronization using mutexes and semaphores
- Zero-Copy: Minimal data copying through perfect forwarding
License
BSD 3-Clause License - See LICENSE file for details
Copyright
Copyright (c) 2021, Siddiq Software LLC. All rights reserved.
- See also
- https://github.com/SiddiqSoft/asynchrony