Structured Concurrency
When your program runs multiple tasks at the same time, you need a way to keep track of them, know when they finish, collect their results, and clean up if something goes wrong. This section shows how to do that.
Join Sets
[![tokio-badge]][tokio]
A JoinSet is a container that holds a group of non-blocking tasks running at the same time.
You add tasks to it, and then you can wait for them to finish one by one. When a task finishes,
you can grab its result. If you throw away the JoinSet before all tasks are done, every
unfinished task is cancelled automatically.
In this recipe, five jobs are created and added to a JoinSet. Each job does some work on its own, and the program waits until all of them are done before moving on.
use JoinSet;
async
Add tokio to Cargo.toml with the macros and rt features enabled.
[dependencies]
tokio = { version = "*", features = ["macros", "rt"] }
{{#include ../links.md}}