---
title: "Bounded buffer in Rust"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/rust/kernel/classic/bounded_buffer/
last_updated: 2026-11-18
sdk_version: 4.44
---
# Bounded buffer in Rust

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

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

The ring and its counts are in one `Mutex`, which holds them only while the semaphores say there is room or an item.

Run:

`pbl samples samples/rust/kernel/classic/bounded_buffer`

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 CAPACITY: usize = 4;const ITEMS\_PER\_PRODUCER: u32 = 50;const NUM\_PRODUCERS: usize = 2;const NUM\_CONSUMERS: usize = 2;struct Ring {items: [u32; CAPACITY],head: usize,tail: usize,used: usize,max\_used: usize,}/// What a message queue does inside: one semaphore counts the free slots,/// another the items, and a mutex guards the ring. Prefer MsgQueue, which/// also works from interrupts.struct Buffer {ring: Mutex\<Ring\>,free\_slots: Semaphore,queued\_items: Semaphore,}impl Buffer {fn put(&self, item: u32) {// Waits for a free slot before the lock, never with it.let \_ = self.free\_slots.take(Timeout::FOREVER);{let mut ring = self.ring.lock();let tail = ring.tail;ring.items[tail] = item;ring.tail = (tail + 1) % CAPACITY;ring.used += 1;ring.max\_used = ring.max\_used.max(ring.used);}self.queued\_items.give();}fn get(&self) -\> u32 {let \_ = self.queued\_items.take(Timeout::FOREVER);let item = {let mut ring = self.ring.lock();let head = ring.head;ring.head = (head + 1) % CAPACITY;ring.used -= 1;ring.items[head]};self.free\_slots.give();item}}static BUFFER: Buffer = Buffer {ring: Mutex::new(Ring {items: [0; CAPACITY],head: 0,tail: 0,used: 0,max\_used: 0,}),free\_slots: Semaphore::new(CAPACITY as u32, CAPACITY as u32),queued\_items: Semaphore::new(0, CAPACITY as u32),};static SUMS: [AtomicU32; NUM\_CONSUMERS] = [const { AtomicU32::new(0) }; NUM\_CONSUMERS];static PRODUCERS: [Thread; NUM\_PRODUCERS] = [const { Thread::new() }; NUM\_PRODUCERS];static CONSUMERS: [Thread; NUM\_CONSUMERS] = [const { Thread::new() }; NUM\_CONSUMERS];fn producer() {for item in 1..=ITEMS\_PER\_PRODUCER {BUFFER.put(item);}}fn consumer(sum: &AtomicU32) {for \_ in 0..ITEMS\_PER\_PRODUCER as usize \* NUM\_PRODUCERS / NUM\_CONSUMERS {sum.fetch\_add(BUFFER.get(), Ordering::Relaxed);// Slower than the producers, so the buffer fills up.thread::yield\_now();}}#[pebbleos::main]fn main() -\> Result\<()\> {let prio = Priority::MAIN.lower(1);let expected = NUM\_PRODUCERS as u32 \* (ITEMS\_PER\_PRODUCER \* (ITEMS\_PER\_PRODUCER + 1) / 2);for t in &PRODUCERS {t.spawn(c"producer", prio, producer)?;}for (t, sum) in CONSUMERS.iter().zip(&SUMS) {t.spawn(c"consumer", prio, move || consumer(sum))?;}for t in PRODUCERS.iter().chain(&CONSUMERS) {t.join(Duration::from\_secs(1))?;}let sum: u32 = SUMS.iter().map(|s| s.load(Ordering::Relaxed)).sum();println!("buffer: {} items produced, consumed items add up to {sum} of {expected}",NUM\_PRODUCERS as u32 \* ITEMS\_PER\_PRODUCER);let ring = BUFFER.ring.lock();println!("buffer: held at most {} of {CAPACITY} items, {} left",ring.max\_used, ring.used);println!("Sample done");Ok(())}
```

## Output

```
buffer: 100 items produced, consumed items add up to 2550 of 2550
buffer: held at most \d of 4 items, 0 left
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/bounded\_buffer.md](https://github.com/coredevices/pebbleos/blob/7753e801d93d20f2d37534ce2fa69d9952015237/docs/samples/rust/kernel/classic/bounded_buffer.md).
