#scheduler #lock-free


A concurrent work-stealing queue for building schedulers

5 releases

0.1.4 Jul 25, 2023
0.1.3 Apr 17, 2021
0.1.2 Apr 7, 2021
0.1.1 Mar 21, 2021
0.1.0 Mar 4, 2021

#201 in Concurrency

Download history 591/week @ 2023-08-05 411/week @ 2023-08-12 363/week @ 2023-08-19 257/week @ 2023-08-26 196/week @ 2023-09-02 201/week @ 2023-09-09 174/week @ 2023-09-16 188/week @ 2023-09-23 206/week @ 2023-09-30 217/week @ 2023-10-07 259/week @ 2023-10-14 163/week @ 2023-10-21 192/week @ 2023-10-28 115/week @ 2023-11-04 40/week @ 2023-11-11 55/week @ 2023-11-18

420 downloads per month


777 lines


A concurrent work-stealing queue for building schedulers.


Distribute some tasks in a thread pool:

use work_queue::{Queue, LocalQueue};

struct Task(Box<dyn Fn(&mut LocalQueue<Task>) + Send>);

let threads = 4;

let queue: Queue<Task> = Queue::new(threads, 128);

// Push some tasks to the queue.
for _ in 0..500 {
    queue.push(Task(Box::new(|local| {

        local.push(Task(Box::new(|_| do_work())));
        local.push(Task(Box::new(|_| do_work())));

// Spawn threads to complete the tasks.
let handles: Vec<_> = queue
    .map(|mut local_queue| {
        std::thread::spawn(move || {
            while let Some(task) = local_queue.pop() {
                task.0(&mut local_queue);

for handle in handles {

Comparison with crossbeam-deque

This crate is similar in purpose to crossbeam-deque, which also provides concurrent work-stealing queues. However there are a few notable differences:

  • This crate is more high level - work stealing is done automatically when calling pop instead of you having to manually call it.
  • As such, we do not support as much customization as crossbeam-deque - but the algorithm itself can be optimized better.
  • Queues have a fixed number of local queues that they support, and this number cannot grow.
  • Each local queue has a fixed capacity, unlike crossbeam-deque which supports local queue growth. This makes our local queues faster.


This crate's queue implementation is based off Tokio's current scheduler. The idea is that each thread holds a fixed-capacity local queue, and there is also an unbounded global queue accessible by all threads. In the general case each worker thread will only interact with its local queue, avoiding lots of synchronization - but if one worker thread happens to have a lot less work than another, it will be spread out evenly due to work stealing.

Additionally, each local queue stores a non-stealable LIFO slot to optimize for message passing patterns, so that if one task creates another, that created task will be polled immediately, instead of only much later when it reaches the front of the local queue.


  • Test it normally using cargo test
  • Test it with Miri using cargo +nightly miri test
  • Test it with ThreadSanitizer using RUSTFLAGS="-Zsanitizer=thread --cfg tsan" cargo +nightly test --tests -Zbuild-std --target={your target triple}
  • Test it with Loom using RUSTFLAGS="--cfg loom" cargo test --tests --release


MIT OR Apache-2.0


~409K SLoC