Async synchronization
Quick answer
Asynchronous synchronization primitives allow tasks to wait for shared resources without blocking an executor thread. Choose the simplest primitive that protects the actual invariant.
Choose a lock or a message
Tokio Mutex uses an asynchronous lock acquisition and permits its guard across await, though long critical sections still delay others. A standard Mutex can be appropriate for short, low-contention, non-awaiting sections. A oneshot channel transfers a single value; watch distributes the latest state, while broadcast distributes messages to subscribers subject to buffer and lag rules.
use std::sync::Arc;use tokio::sync::Mutex;#[tokio::main(flavor = "current_thread")]async fn main() { let value = Arc::new(Mutex::new(0)); let other = value.clone(); let worker = tokio::spawn(async move { *other.lock().await += 1; }); *value.lock().await += 2; worker.await.unwrap(); println!("{}", *value.lock().await);}Manual local Rust/Cargo candidate; not executed. Expected behavior:
3Both modifications hold the same mutex.
#[tokio::main(flavor = "current_thread")]async fn main() { let (tx, rx) = tokio::sync::oneshot::channel(); tx.send(42).unwrap(); println!("{}", rx.await.unwrap());}Manual local Rust/Cargo candidate; not executed. Expected behavior:
42The receiver awaits a single result rather than repeatedly checking shared state.
Common mistake
Do not hold an async mutex across unrelated slow operations merely because it is permitted.
Try it yourself
Replace the shared counter with one owner task receiving increments through a bounded channel.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Async synchronization — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
An async task only needs to protect a tiny non-awaiting update. What is a reasonable mutex choice?
Async synchronization — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::sync::Arc; use tokio::sync::Mutex; #[tokio::main(flavor = "current_thread")] async fn main() { let value = Arc::new(Mutex::new(0)); let other = value.clone(); let worker = tokio::spawn(async move { *other.lock().await += 1; }); *value.lock().await += 2; worker.await.unwrap(); println!("{}", *value.lock().await); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Async synchronization — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust #[tokio::main(flavor = "current_thread")] async fn main() { let (tx, rx) = tokio::sync::oneshot::channel(); tx.send(42).unwrap(); println!("{}", rx.await.unwrap()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.