---
title: "Readers and writers in Rust"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/rust/kernel/classic/readers_writers/
last_updated: 2026-11-18
sdk_version: 4.44
---
# Readers and writers in Rust

Source: https://developer.repebble.com/pebbleos/docs/samples/rust/kernel/classic/readers_writers/

The [readers and writers sample](https://developer.repebble.com/pebbleos/docs/samples/kernel/classic/readers_writers.md) written in [Rust](https://developer.repebble.com/pebbleos/docs/development/build_system/rust.md), on the `pebbleos` crate.

The lock hands out guards, which leave the room when dropped.

Run:

`pbl samples samples/rust/kernel/classic/readers_writers`

Boards:

every board

## Sources

src/lib.rs

```
#![no\_std]use core::sync::atomic::{AtomicU32, Ordering};use core::time::Duration;use pebbleos::sync::{Mutex, Semaphore};use pebbleos::thread::{self, Priority, Thread};use pebbleos::time::Timeout;use pebbleos::{Result, println};const NUM\_READERS: usize = 3;const READS: u32 = 10;const WRITES: u32 = 5;/// Any number of readers, or one writer. The room is a semaphore and not a/// mutex because the reader that leaves it last is not always the one that/// entered first.struct RwLock {readers: Mutex\<u32\>,room: Semaphore,}struct ReadGuard\<'a\>(&'a RwLock);struct WriteGuard\<'a\>(&'a RwLock);impl RwLock {const fn new() -\> RwLock {RwLock {readers: Mutex::new(0),room: Semaphore::new(1, 1),}}fn read(&self) -\> ReadGuard\<'\_\> {let mut readers = self.readers.lock();\*readers += 1;if \*readers == 1 {// The first reader in takes the room for all of them.let \_ = self.room.take(Timeout::FOREVER);}ReadGuard(self)}fn write(&self) -\> WriteGuard\<'\_\> {let \_ = self.room.take(Timeout::FOREVER);WriteGuard(self)}}impl Drop for ReadGuard\<'\_\> {fn drop(&mut self) {let mut readers = self.0.readers.lock();\*readers -= 1;if \*readers == 0 {self.0.room.give();}}}impl Drop for WriteGuard\<'\_\> {fn drop(&mut self) {self.0.room.give();}}/// Written in two steps, which no reader may see half done.struct Record {version: AtomicU32,copy\_of\_version: AtomicU32,}struct Stats {reading: u32,max\_reading: u32,torn\_reads: u32,writes\_with\_readers: u32,}static LOCK: RwLock = RwLock::new();static RECORD: Record = Record {version: AtomicU32::new(0),copy\_of\_version: AtomicU32::new(0),};// Readers share the room, so what they count needs a lock of its own.static STATS: Mutex\<Stats\> = Mutex::new(Stats {reading: 0,max\_reading: 0,torn\_reads: 0,writes\_with\_readers: 0,});static READERS: [Thread; NUM\_READERS] = [const { Thread::new() }; NUM\_READERS];static WRITER: Thread = Thread::new();fn reader() {for \_ in 0..READS {let guard = LOCK.read();{let mut stats = STATS.lock();stats.reading += 1;stats.max\_reading = stats.max\_reading.max(stats.reading);}let version = RECORD.version.load(Ordering::Relaxed);thread::sleep(Duration::from\_millis(2));let torn = version != RECORD.copy\_of\_version.load(Ordering::Relaxed);{let mut stats = STATS.lock();stats.torn\_reads += torn as u32;stats.reading -= 1;}drop(guard);// Out of the room for a while, or a writer would never find it empty.thread::sleep(Duration::from\_millis(3));}}fn writer() {for \_ in 0..WRITES {let guard = LOCK.write();{let mut stats = STATS.lock();if stats.reading != 0 {stats.writes\_with\_readers += 1;}}let version = RECORD.version.fetch\_add(1, Ordering::Relaxed) + 1;thread::sleep(Duration::from\_millis(2));RECORD.copy\_of\_version.store(version, Ordering::Relaxed);drop(guard);thread::sleep(Duration::from\_millis(5));}}#[pebbleos::main]fn main() -\> Result\<()\> {let prio = Priority::MAIN.lower(1);for t in &READERS {t.spawn(c"reader", prio, reader)?;}WRITER.spawn(c"writer", prio, writer)?;for t in READERS.iter().chain([&WRITER]) {t.join(Duration::from\_secs(2))?;}let stats = STATS.lock();println!("record: version {} after {WRITES} writes",RECORD.version.load(Ordering::Relaxed));println!("record: up to {} readers at once, {} half written records read",stats.max\_reading, stats.torn\_reads);println!("record: {} writes made with a reader in the room",stats.writes\_with\_readers);println!("Sample done");Ok(())}
```

## Output

```
record: version 5 after 5 writes
record: up to 3 readers at once, 0 half written records read
record: 0 writes made with a reader in the room
Sample done
```

Generated from PebbleOS [7753e801d](https://github.com/coredevices/pebbleos/commit/7753e801d93d20f2d37534ce2fa69d9952015237). This page is maintained in the pebbleos repository: [docs/samples/rust/kernel/classic/readers\_writers.md](https://github.com/coredevices/pebbleos/blob/7753e801d93d20f2d37534ce2fa69d9952015237/docs/samples/rust/kernel/classic/readers_writers.md).
