Installation
Using CMake (Recommended)
Add the library to your CMakeLists.txt:
include(FetchContent)
FetchContent_Declare(asynchrony
GIT_REPOSITORY https://github.com/SiddiqSoft/asynchrony.git
GIT_TAG main
)
FetchContent_MakeAvailable(asynchrony)
target_link_libraries(your_target PRIVATE asynchrony::asynchrony)
Using NuGet (Windows)
nuget install SiddiqSoft.asynchrony
Compiler Setup
Visual Studio 2019 or later
- Set C++ Language Standard to /std:c++20 or /std:c++latest
- No additional flags required
GCC 10+
g++ -std=c++20 -pthread your_file.cpp
Clang 10+
clang++ -std=c++20 -fexperimental-library -pthread your_file.cpp
Your First Program
Create a simple program that uses the asynchrony library:
#include <iostream>
#include <chrono>
#include <format>
#include "siddiqsoft/simple_worker.hpp"
struct PrintTask {
std::string message;
void operator()() {
std::cout << "Processing: " << message << std::endl;
}
};
int main() {
[](auto&& task) {
task();
}
};
for (int i = 0; i < 5; ++i) {
worker.
queue(PrintTask{
"Task " + std::to_string(i)});
}
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "Done!" << std::endl;
return 0;
}
Implements a simple queue + semaphore driven asynchronous processor.
void queue(T &&item)
Queue item into this worker thread's deque.
Common Patterns
Pattern 1: Fire and Forget
Queue work and let it process asynchronously:
worker.
queue(std::move(task));
Pattern 2: Batch Processing
Process multiple items in parallel:
for (auto& item : items) {
pool.
queue(std::move(item));
}
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).
Pattern 3: Periodic Tasks
Execute a function at regular intervals:
[]() { std::cout << "Tick!" << std::endl; },
std::chrono::milliseconds(1000)
};
Implements a simple queue + semaphore driven asynchronous processor.
Pattern 4: Resource Pool
Manage a pool of reusable resources:
auto conn = connPool.checkout();
conn.execute("SELECT * FROM users");
Implements a resource pool that stores objects of type T. Said objects can be shared_ptr or unique_pt...
void checkin(T &&rsrc)
Insert a new element or return a borrowed element.
Troubleshooting
Compilation Errors
Error: 'jthread' is not a member of 'std'
- Solution: Ensure you're using C++20 or later. Update your compiler flags to -std=c++20 or /std:c++20.
Error: undefined reference to pthread_*
- Solution: Link against pthread library: -pthread flag or target_link_libraries(... pthread)
Error: 'stop_token' is not a member of 'std'
- Solution: Ensure your compiler supports C++20. Update to GCC 10+, MSVC 16.11+, or Clang 10+.
Runtime Issues
Issue: Tasks not executing
- Solution: Ensure the worker/pool object is not destroyed before tasks complete
- Solution: Check that the callback function is valid and doesn't throw uncaught exceptions
Issue: High CPU usage
- Solution: Reduce the number of threads in the pool
- Solution: Increase the wait timeout in the worker configuration
Issue: Deadlock or hanging
- Solution: Ensure callbacks don't block indefinitely
- Solution: Avoid circular dependencies between workers
Next Steps