---
title: "Work queues in Rust"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/rust/kernel/work/
last_updated: 2026-11-18
sdk_version: 4.44
---
# Work queues in Rust

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

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

`Work<T>` holds the data its function gets, in place of the C structure that embeds the item; `WorkQueue::start()` runs a queue on a `Thread` of its own.

Run:

`pbl samples samples/rust/kernel/work`

Boards:

every board

## Sources

src/lib.rs

```
#![no\_std]use core::sync::atomic::{AtomicU32, Ordering};use pebbleos::thread::{self, Priority, Thread};use pebbleos::work::{Work, WorkQueue};use pebbleos::{Result, println};// What the work item acts on, which its function gets through it.struct Sensor {name: &'static str,pending\_events: AtomicU32,runs: AtomicU32,}impl Sensor {const fn new(name: &'static str) -\> Sensor {Sensor {name,pending\_events: AtomicU32::new(0),runs: AtomicU32::new(0),}}}fn sensor\_work(sensor: &'static Work\<Sensor\>) {// One run serves every event since the last one, however often it was// submitted.println!("{}: handling {} events on {}",sensor.name,sensor.pending\_events.swap(0, Ordering::Relaxed),thread::current().name());sensor.runs.fetch\_add(1, Ordering::Relaxed);}fn sensor\_event(q: &'static WorkQueue, sensor: &'static Work\<Sensor\>) {sensor.pending\_events.fetch\_add(1, Ordering::Relaxed);if !sensor.submit(q) {println!("{}: already pending, not queued again", sensor.name);}}static ACCEL: Work\<Sensor\> = Work::new(sensor\_work, Sensor::new("accel"));static TOUCH: Work\<Sensor\> = Work::new(sensor\_work, Sensor::new("touch"));// A queue of its own keeps slow work from delaying the system's.static SENSOR\_Q: WorkQueue = WorkQueue::new();static SENSOR\_Q\_THREAD: Thread = Thread::new();#[pebbleos::main]fn main() -\> Result\<()\> {let system = WorkQueue::system();// Both queues run below main, so nothing runs until main waits for them.println!("main: three accel events for the system queue");for \_ in 0..3 {sensor\_event(system, &ACCEL);}system.flush();println!("main: accel work ran {} time(s)",ACCEL.runs.load(Ordering::Relaxed));println!("main: touch and accel events for a queue of our own");SENSOR\_Q.start(&SENSOR\_Q\_THREAD, c"sensor\_q", Priority::MAIN.lower(1))?;sensor\_event(&SENSOR\_Q, &TOUCH);sensor\_event(&SENSOR\_Q, &ACCEL);SENSOR\_Q.flush();println!("main: a touch event, canceled before the queue gets to it");sensor\_event(&SENSOR\_Q, &TOUCH);if TOUCH.cancel\_sync() {println!("main: touch work was pending and will not run");}SENSOR\_Q.flush();println!("main: touch work ran {} time(s)",TOUCH.runs.load(Ordering::Relaxed));println!("Sample done");Ok(())}
```

## Output

```
accel: already pending, not queued again
accel: already pending, not queued again
accel: handling 3 events on SysWorkq
main: accel work ran 1 time\(s\)
touch: handling 1 events on sensor_q
accel: handling 1 events on sensor_q
main: touch work was pending and will not run
main: touch work ran 1 time\(s\)
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/work.md](https://github.com/coredevices/pebbleos/blob/7753e801d93d20f2d37534ce2fa69d9952015237/docs/samples/rust/kernel/work.md).
