A reader report records shared-read coordination. This program takes a selected number of RwLock read guards, probes try_write, and reports the shared-read status.

Program

Play the program to choose how many readers hold the lock and watch the write probe and status change.

rwlock_read_coordination_report.rs
use std::sync::RwLock;

fn main() {
    let readers = ;
    let lock = RwLock::new(7);
    let mut guards = Vec::new();
    for _ in 0..readers {
        if let Ok(guard) = lock.try_read() {
            guards.push(guard);
        }
    }
    let active = guards.len();
    let write_blocked = lock.try_write().is_err();
    let status = if active == 0 {
        "writable"
    } else if active == 1 {
        "single"
    } else {
        "shared"
    };
    println!("readers={active} write_blocked={write_blocked} {status}");
}
use std::sync::RwLock;

fn main() {
    let readers = ;
    let lock = RwLock::new(7);
    let mut guards = Vec::new();
    for _ in 0..readers {
        if let Ok(guard) = lock.try_read() {
            guards.push(guard);
        }
    }
    let active = guards.len();
    let write_blocked = lock.try_write().is_err();
    let status = if active == 0 {
        "writable"
    } else if active == 1 {
        "single"
    } else {
        "shared"
    };
    println!("readers={active} write_blocked={write_blocked} {status}");
}
use std::sync::RwLock;

fn main() {
    let readers = ;
    let lock = RwLock::new(7);
    let mut guards = Vec::new();
    for _ in 0..readers {
        if let Ok(guard) = lock.try_read() {
            guards.push(guard);
        }
    }
    let active = guards.len();
    let write_blocked = lock.try_write().is_err();
    let status = if active == 0 {
        "writable"
    } else if active == 1 {
        "single"
    } else {
        "shared"
    };
    println!("readers={active} write_blocked={write_blocked} {status}");
}
shared reads `RwLock::try_read` grants multiple read guards at once, so several readers can observe the value together.
write exclusion `try_write` is rejected while any read guard is held, so the report shows the write as blocked.
reader count The number of live read guards drives the coordination status from writable through shared.