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

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

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

The forks are `Mutex<()>`, and a philosopher puts one down by dropping its guard.

Run:

`pbl samples samples/rust/kernel/classic/dining_philosophers`

Boards:

every board

## Sources

src/lib.rs

```
#![no\_std]use core::sync::atomic::{AtomicBool, 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\_PHILOSOPHERS: usize = 5;const MEALS: u32 = 3;struct Philosopher {thread: Thread,meals: AtomicU32,stuck: AtomicBool,}static FORKS: [Mutex\<()\>; NUM\_PHILOSOPHERS] = [const { Mutex::new(()) }; NUM\_PHILOSOPHERS];static PHILOSOPHERS: [Philosopher; NUM\_PHILOSOPHERS] = [const {Philosopher {thread: Thread::new(),meals: AtomicU32::new(0),stuck: AtomicBool::new(false),}}; NUM\_PHILOSOPHERS];static HOLDING\_ONE: AtomicU32 = AtomicU32::new(0);// Stuck philosophers keep their fork until main has counted them.static GIVE\_UP: Semaphore = Semaphore::new(0, NUM\_PHILOSOPHERS as u32);fn dine(seat: usize, ordered: bool) {let me = &PHILOSOPHERS[seat];let mut first = seat;let mut second = (seat + 1) % NUM\_PHILOSOPHERS;// The fix: everyone takes the lower numbered fork first, so no cycle of// waiters can form.if ordered && first \> second {core::mem::swap(&mut first, &mut second);}while me.meals.load(Ordering::Relaxed) \< MEALS {let first\_fork = FORKS[first].lock();// A deadlock needs bad luck with the timing, which is staged here:// nobody goes on until every philosopher holds a fork. With the fix// they cannot all hold one.HOLDING\_ONE.fetch\_add(1, Ordering::Relaxed);loop {thread::yield\_now();if ordered || HOLDING\_ONE.load(Ordering::Relaxed) as usize \>= NUM\_PHILOSOPHERS {break;}}// The time limit stands in for the deadlock, which would otherwise// end the sample here.let Ok(second\_fork) = FORKS[second].lock\_timeout(Duration::from\_millis(50)) else {me.stuck.store(true, Ordering::Relaxed);let \_ = GIVE\_UP.take(Timeout::FOREVER);return;};me.meals.fetch\_add(1, Ordering::Relaxed);thread::sleep(Duration::from\_millis(2));HOLDING\_ONE.fetch\_sub(1, Ordering::Relaxed);drop(second\_fork);drop(first\_fork);}}fn dinner(ordered: bool) -\> Result\<()\> {HOLDING\_ONE.store(0, Ordering::Relaxed);for (seat, p) in PHILOSOPHERS.iter().enumerate() {p.meals.store(0, Ordering::Relaxed);p.stuck.store(false, Ordering::Relaxed);p.thread.spawn(c"philosopher", Priority::MAIN.lower(1), move || {dine(seat, ordered)})?;}// Long enough for every meal, or for every time limit to pass.thread::sleep(Duration::from\_millis(200));let mut meals = 0;let mut stuck = 0;for p in &PHILOSOPHERS {meals += p.meals.load(Ordering::Relaxed);stuck += p.stuck.load(Ordering::Relaxed) as u32;GIVE\_UP.give();}for p in &PHILOSOPHERS {p.thread.join(Duration::from\_secs(1))?;}GIVE\_UP.reset();println!("dinner: {}: {meals} meals eaten, {stuck} of {NUM\_PHILOSOPHERS} philosophers stuck with one fork",if ordered {"lower fork first"} else {"left fork first"});Ok(())}#[pebbleos::main]fn main() -\> Result\<()\> {dinner(false)?;dinner(true)?;println!("Sample done");Ok(())}
```

## Output

```
dinner: left fork first: 0 meals eaten, 5 of 5 philosophers stuck with one fork
dinner: lower fork first: 15 meals eaten, 0 of 5 philosophers stuck with one fork
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/dining\_philosophers.md](https://github.com/coredevices/pebbleos/blob/7753e801d93d20f2d37534ce2fa69d9952015237/docs/samples/rust/kernel/classic/dining_philosophers.md).
