asynchrony 0.0.0
Add asynchrony to your C++ applications using standard C++20
Loading...
Searching...
No Matches
asynchrony - Add Asynchrony to Your C++ Applications

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

Component Description
siddiqsoft::simple_worker Single-threaded asynchronous processor
siddiqsoft::simple_pool Multi-threaded pool with shared queue
siddiqsoft::roundrobin_pool Multi-threaded pool with per-thread queues
siddiqsoft::periodic_worker Periodic task executor
siddiqsoft::resource_pool Resource pool manager

Quick Start

Simple Worker Example

#include "siddiqsoft/simple_worker.hpp"
struct MyTask {
std::string data;
void operator()() { /* process data */ }
};
int main() {
siddiqsoft::simple_worker<MyTask> worker{[](auto& task) {
task(); // Execute the task
}};
// Queue work
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() {
siddiqsoft::simple_pool<MyTask> pool{[](auto& task) {
task(); // Execute the task
}};
// Queue work across multiple threads
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