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

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

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

The shop’s counts are in one `Mutex`; `Thread::state()` tells the barber is asleep.

Run:

`pbl samples samples/rust/kernel/classic/sleeping_barber`

Boards:

every board

## Sources

src/lib.rs

```
#![no\_std]use core::time::Duration;use pebbleos::sync::{Mutex, Semaphore};use pebbleos::thread::{self, Priority, State, Thread};use pebbleos::time::{self, Timeout};use pebbleos::{Result, println};const NUM\_CHAIRS: u32 = 3;const NUM\_CUSTOMERS: usize = 5;// Customers run above the barber, so all of them arrive before the first// haircut starts.const CUSTOMER\_PRIO: Priority = Priority::MAIN.lower(1);const BARBER\_PRIO: Priority = Priority::MAIN.lower(2);struct Shop {waiting: u32,haircuts: u32,turned\_away: u32,}// Counts the customers waiting, which is what the barber sleeps on.static CUSTOMERS\_WAITING: Semaphore = Semaphore::new(0, NUM\_CHAIRS);// Given by the barber when they are ready for the next one.static BARBER\_CHAIR: Semaphore = Semaphore::new(0, 1);static SHOP: Mutex\<Shop\> = Mutex::new(Shop {waiting: 0,haircuts: 0,turned\_away: 0,});static BARBER: Thread = Thread::new();static CUSTOMERS: [Thread; NUM\_CUSTOMERS] = [const { Thread::new() }; NUM\_CUSTOMERS];fn barber() {loop {// Asleep until a customer sits down in the waiting room.let \_ = CUSTOMERS\_WAITING.take(Timeout::FOREVER);SHOP.lock().waiting -= 1;BARBER\_CHAIR.give();thread::sleep(Duration::from\_millis(20));SHOP.lock().haircuts += 1;}}fn customer(id: usize) {// Looking for a chair and taking it is one step, or two customers could// take the last one.let seated = {let mut shop = SHOP.lock();let seated = shop.waiting \< NUM\_CHAIRS;if seated {shop.waiting += 1;} else {shop.turned\_away += 1;}seated};if !seated {println!("customer {id}: no free chair, leaves");return;}CUSTOMERS\_WAITING.give();let \_ = BARBER\_CHAIR.take(Timeout::FOREVER);println!("customer {id}: in the barber chair");}#[pebbleos::main]fn main() -\> Result\<()\> {BARBER.spawn(c"barber", BARBER\_PRIO, barber)?;thread::sleep(Duration::from\_millis(10));println!("shop: empty, barber asleep: {}",yes\_no(BARBER.state() == State::Blocked));println!("shop: {NUM\_CUSTOMERS} customers arrive at once, for {NUM\_CHAIRS} chairs");for (id, t) in CUSTOMERS.iter().enumerate() {t.spawn(c"customer", CUSTOMER\_PRIO, move || customer(id))?;}time::poll(Duration::from\_millis(5), Duration::from\_secs(1), || {let shop = SHOP.lock();shop.haircuts + shop.turned\_away == NUM\_CUSTOMERS as u32})?;thread::sleep(Duration::from\_millis(10));let shop = SHOP.lock();println!("shop: {} haircuts, {} customers turned away",shop.haircuts, shop.turned\_away);drop(shop);println!("shop: empty, barber asleep: {}",yes\_no(BARBER.state() == State::Blocked));println!("Sample done");Ok(())}fn yes\_no(b: bool) -\> &'static str {if b { "yes" } else { "no" }}
```

## Output

```
shop: empty, barber asleep: yes
shop: 5 customers arrive at once, for 3 chairs
customer \d: no free chair, leaves
customer \d: no free chair, leaves
customer \d: in the barber chair
customer \d: in the barber chair
customer \d: in the barber chair
shop: 3 haircuts, 2 customers turned away
shop: empty, barber asleep: yes
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/sleeping\_barber.md](https://github.com/coredevices/pebbleos/blob/7753e801d93d20f2d37534ce2fa69d9952015237/docs/samples/rust/kernel/classic/sleeping_barber.md).
