https://github.com/softprops/waitout
awaits the completion of multiple async tasks
https://github.com/softprops/waitout
Last synced: about 1 year ago
JSON representation
awaits the completion of multiple async tasks
- Host: GitHub
- URL: https://github.com/softprops/waitout
- Owner: softprops
- License: mit
- Created: 2015-11-17T06:31:39.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-11-29T01:41:46.000Z (over 10 years ago)
- Last Synced: 2025-03-17T12:00:05.724Z (over 1 year ago)
- Language: Rust
- Homepage:
- Size: 512 KB
- Stars: 12
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# waitout
[](https://travis-ci.org/softprops/waitout)
Waitout provides a simple interface for tracking and awaiting the completion of multiple
asynchounous tasks.
## api docs
Find them [here](https://softprops.github.io/waitout).
## install
Add the following to you're `Cargo.toml` file
```toml
[dependencies]
waitout = "0.1"
```
## usage
It's sometimes useful to fan out independant tasks asynchronously for efficient completion of
an aggregate task. Asynchronous tasks may sometimes be staged in various scopes making it difficult
to monitor the current state of their completion. Some languages have std library interfaces like [CountDownLatches](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CountDownLatch.html) and [WaitGroups](https://golang.org/pkg/sync/#WaitGroup) to help solve this problem. Absent of a similar interface in rust, the motivation for waitout was born.
Waitout is a simple wrapper around of few rust synchronisation primitives that make staged task completion more straight forward.
The idea is simple, keep reference create a shared counter that increments for every task you wish to wait on.
When a task completes decrement that counter. When the counter reaches 0, the current thread may proceed.
```rust
extern crate waitout;
use std::sync::Arc;
use std::thread;
use waitout::WaitGroup;
fn main() {
let wg = Arc::new(
WaitGroup::new(0)
);
for _ in 0..100 {
wg.add(1);
let wg2 = wg.clone();
thread::spawn(move|| {
thread::sleep_ms(2000);
wg2.done();
});
}
wg.wait();
println!("all set")
}
```
Doug Tangren (softprops) 2015