Tokio

Mastering Concurrency in Rust: Tokio Structured Concurrency and Async Patterns

Published: June 15, 2024 Reading time: 9 min

tokio::spawn looks like structured concurrency. It is not. You get a JoinHandle. If you await it, you wait for that one task. If you drop it, the task keeps running on the runtime. If the function that spawned it returns, the work is still there. That is unstructured concurrency: the child is not owned by the scope that created it. Structured concurrency is the opposite rule. Child tasks belong to a scope. Leaving the scope waits for them or cancels them. Tokio does not give you Kotlin-style coroutineScope, but it does give you the pieces: JoinSet (abort-on-drop), join! / try_join! / select! for futures in the current task, and CancellationToken when abort is too blunt. ...

Continue Reading