expression

Async shared reads and exclusive writes

A read-write lock admits several readers but grants a writer exclusive access. Release reader guards before requesting a writer; attempting an implicit upgrade while retaining a read guard can deadlock. Fair admission may delay readers when a writer waits. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 10 10 11 Common mistake: Attempting to acquire write while still retaining a read guard.

Formal Syntax & Structure

rust syntaxrust
lock.read().await; lock.write().await

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    let lock = tokio::sync::RwLock::new(10);    let a = lock.read().await;    let b = lock.read().await;    println!("{} {}", *a, *b);    drop(a);    drop(b);    *lock.write().await += 1;    println!("{}", *lock.read().await);}
Rust Reference