Crate taskwait[−][src]
This library provides facility to wait on multiple spawned async tasks. This is runtime agnostic.
Example using adding and waiting on tasks
use taskwait::TaskGroup; #[tokio::main] async fn main() { let tg = TaskGroup::new(); for _ in 0..10 { tg.add(1); let tg_c = tg.clone(); tokio::spawn(async move{ //... tg_c.done(); }); } tg.wait().await; }
Note: User must ensure that call to done() made above is made in both success & error code path.
Example using add & auto_work
This example uses auto_work() which creates a Work
object. When it goes out of scope done() is
is automatically called.
use taskwait::TaskGroup; #[tokio::main] async fn main() { let tg = TaskGroup::new(); for _ in 0..10 { tg.add(1); let work = tg.auto_work(); tokio::spawn(async move{ let _work = work; // done() will be called when this is dropped //... }); } tg.wait().await; }
Example using work
This example uses work() which creates a Work
object. This calls TaskGroup::add
. When it goes out of scope done() is
is automatically called.
use taskwait::TaskGroup; #[tokio::main] async fn main() { let tg = TaskGroup::new(); for _ in 0..10 { let work = tg.work(); tokio::spawn(async move{ let _work = work; // done() will be called when this is dropped //... }); } tg.wait().await; }
Structs
TaskGroup | |
WaitFuture | Future to wait for the counter to become 0 or -ve. |
Work | Represents a work or task. |