On this page
  1. RwLock
  2. Signature & Overview
  3. Notes & Pitfalls
  4. Example

RwLock

Prev: `Mutex` | Up: sync | Next: `mpsc`
Rel: sync, rust/builtins
Ref: Rust std — RwLock
Tags: #public #prog #rust #stdlib #sync #tier/standard


Signature & Overview

RwLock<T>

Allows many concurrent readers (.read()) OR one exclusive writer (.write()).

Notes & Pitfalls

Example

use std::sync::RwLock;

fn main() {
    let lock = RwLock::new(vec![1, 2]);
    {
        let r1 = lock.read().unwrap();
        let r2 = lock.read().unwrap();
        assert_eq!(r1.len() + r2.len(), 4);
    }
    lock.write().unwrap().push(3);
    assert_eq!(lock.read().unwrap().len(), 3);
}

Linked from

Related notes