Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/heapwolf/cpp-libasync
general purpose asynchronous control flow helpers on thread-safe data structures
https://github.com/heapwolf/cpp-libasync
Last synced: 25 days ago
JSON representation
general purpose asynchronous control flow helpers on thread-safe data structures
- Host: GitHub
- URL: https://github.com/heapwolf/cpp-libasync
- Owner: heapwolf
- Created: 2015-05-17T13:56:13.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-05-17T17:13:11.000Z (over 9 years ago)
- Last Synced: 2024-10-03T08:38:33.016Z (about 1 month ago)
- Language: C++
- Size: 133 KB
- Stars: 9
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SYNOPSIS
This is an idea for some general purpose asynchronous control flow helpers
that write to a data structure of the given input type in a thread-safe way.# BUILD
```
c++ test.cc -o test -std=c++1y
```# METHODS
## EACH PARALLEL
Run the function in parallel on each of the array values, each without
waiting until the previous function has completed. The calling thread
waits until all tasks have completed.
```cpp
#include "libsync.h"//
// any arbitrary iterable
//
vector arr { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };auto results = libasync.eachParallel(arr, [&](auto value, auto push) {
// do something async
push(value);
});for(auto &el : results)
cout << el << endl;
```## EACH
Run the function on each item in the array in series, each one running
once the previous function has completed. For each item the lambda is
executed in a separate thread. If `push` is not called, the iteration
will stop. You can also terminate flow by passing `nullptr` as the
value to `push`.```cpp
vector arr { "foo", "bar", "bazz" };auto results = libasync.each(arr, [&](auto value, auto push) {
push(value);
});for (auto &el : results)
cout << el << endl;
```## ERRORS
Methods can be wrapped in try catches! The thread will bubble the
exception back up to the caller.```cpp
try {vector arr { "foo", "bar", "bazz" };
libasync.each(arr, [&](auto value, auto push) {
if (value == "bar") throw runtime_error("oops");
push(value);
});}
catch(const std::exception& ex) {
cout << ex.what() << endl;
}
```