std-type

RwLock: shared reads and exclusive writes

A read-write lock permits several readers when no writer holds it. Writers need exclusive access. Its usefulness depends on workload; scheduling and fairness are platform-dependent. Expected stdout (not executed): 8 9 Common mistake: Assuming a read-write lock always outperforms a mutex.

Formal Syntax & Structure

rust syntaxrust
RwLock::new(value)

Working Example

Example Snippetrust
fn main() {    use std::sync::RwLock;    let state = RwLock::new(4);    { let a = state.read().unwrap(); let b = state.read().unwrap(); println!("{}", *a + *b); }    *state.write().unwrap() = 9;    println!("{}", *state.read().unwrap());}
Rust Reference