9.1 Work Stealing

SCHEDULINGSTATUS: STABLELEVEL: DEEPREVIEWED: 2026-11-18
Before this page Thread Pools, Atomics

9.1.1 Motivation

Parallel programs must distribute work fairly across CPUs. A single global work queue serves all workers, but every dequeue contends for a lock. Per-worker queues remove that bottleneck and create a new problem: some workers exhaust their queue while others still have tasks ready. Work stealing lets idle workers take tasks from busy ones. The protocol keeps the owner’s own pushes and pops cheap and puts the synchronization cost on the thief, which only pays it when it steals.

9.1.2 The problem

Imagine four CPU cores, each running a thread. Each thread manages its own queue. Core A finishes its queue at time 2 ms. Core B finishes at time 7 ms. Core C holds work until time 15 ms. Core D holds work until time 20 ms. While cores A and B remain idle, cores C and D burn CPU. Total time: 20 ms. The four cores did 2 + 7 + 15 + 20 = 44 ms of work between them, so an even split would have finished in 11 ms.

a step ends when its slowest worker does core A 2 ms idle 18 ms core B 7 ms idle 13 ms core C 15 ms idle 5 ms core D 20 ms, never idle an even split ends here, 11 ms the step actually ends here, 20 ms

The accented stretches are capacity that was bought and not used. Work stealing exists to move tasks into them.

A single global queue lets cores A and B grab work from C and D. But a global queue means every dequeue does this: acquire a lock, pop the task, release the lock. That lock is a synchronization bottleneck. With many cores and many tiny tasks, lock contention can eat the speedup.

Per-worker queues avoid the contention by giving each worker its own queue, so a worker pushes and pops without taking a lock. On their own they bring the imbalance back, which is the gap work stealing fills.

9.1.3 Mental model

In work stealing, each worker owns a deque (double-ended queue). The owner pushes tasks to one end and pops tasks from the same end. An idle worker becomes a thief: it picks a busy worker’s deque and takes a task from the other end.

The owner pops from the “warm” end: the task it pushed last is likely to reuse data still in the CPU cache. The oldest tasks, at the far end, likely belong to a different part of the work tree. In fork-join parallelism, the oldest tasks are often the root of a large subtree, so a thief that takes one gets a large, independent piece of work and does not need to steal again soon. The owner works LIFO and thieves take FIFO.

9.1.4 A simple example

Three workers manage a fork-join workload. Worker 1 pushed tasks A, B and C, in that order, so A is the oldest. Worker 2 holds no tasks. Worker 3 holds task D and is still busy. At time T, Worker 2 becomes idle. It picks Worker 1 as the victim at random and steals task A from the far end of Worker 1’s deque. Worker 1 keeps popping C, then B, from its own end.

flowchart LR
  subgraph W1["Worker 1"]
    direction LR
    Q1["[A, B, C]<br/>A at the far end"]
  end
  subgraph W2["Worker 2"]
    direction LR
    Q2["[ ] idle"]
  end
  subgraph W3["Worker 3"]
    direction LR
    Q3["[D] busy"]
  end

  Q1 -->|worker 2 steals A| Q2
  W2 ~~~ W3

Three worker queues. Worker 2 steals task A, the oldest, from the far end of Worker 1’s queue.

The pseudocode for a steal attempt from Worker 1 by Worker 2:

victim_tail = load(Worker1.tail)        // far end
victim_head = load(Worker1.head)        // warm end
if victim_tail < victim_head:
  task = load(Worker1.queue[victim_tail])
  if compare_and_swap(Worker1.tail, victim_tail, victim_tail + 1):
    return task  // steal won
  // else: another thief, or the owner, took that task first
return null  // queue empty, or the race was lost

The owner, Worker 1, pushes and pops at head. The thief, Worker 2, loads tail and tries to increment it with a compare-and-swap (CAS). If two thieves race for the same task, one CAS succeeds and the other thief retries elsewhere.

tail: thieves steal from this end head: the owner pushes and pops at this end worker 1 A B C worker 2 worker 3 D worker 1 keeps B and C, its newest tasks, still cache-warm worker 2 is idle, so it steals the oldest task, A

The owner pushes and pops at the head of its deque, and an idle worker steals the oldest task from the tail of another worker’s deque.

9.1.5 Mechanism

Intuition

Work stealing creates a two-phase pattern. In phase 1, workers build up per-worker queues during task spawning. Workers mostly use their own queue. Contention is low. In phase 2, the program reaches an imbalance. Some workers empty their queue before others finish. Idle workers steal from busy workers. Stealing is rare and localized: only the victim and the thief contend on that queue. Picking victims at random spreads the steals across workers.

Mechanics

Each worker’s deque has two atomic indices. The owner pushes and pops at the head; thieves steal at the tail. The protocol:

  1. Owner pushes a task at the head.
  2. Owner pops a task from the head.
  3. A thief loads the tail, checks that tail < head, and steals with a CAS on the tail.
  4. If the CAS succeeds, the thief has the task. If it fails, another thief or the owner took it first.

Only the owner ever writes head. Thieves read both indices but write only the tail, and only through a CAS. That asymmetry keeps the owner’s common case free of atomic read-modify-write operations.

Two thieves can read the same tail and both try to increment it; the CAS lets exactly one succeed, and the other retries elsewhere. The harder case is the last task, where the owner’s pop and a thief’s steal both want the same element. There the owner also has to use a CAS on the tail, as the next section shows.

Runtime detail: The Chase-Lev deque

The Chase-Lev deque, named after David Chase and Yossi Lev (SPAA 2005), is a lock-free deque designed for work stealing. It builds on the earlier deque of Arora, Blumofe and Plaxton, which used a fixed-size array and could overflow. Chase and Lev store the tasks in a circular array that grows when it fills, so the deque has no fixed capacity. The paper calls the owner’s end “bottom” and the thieves’ end “top”; the sketch below keeps this page’s names, head for the owner’s end and tail for the thieves’. The tail only ever grows, because a successful steal advances it by one; a pop lowers the head and puts it back if the deque turns out to be empty. Each index maps to a slot modulo the array size.

struct ChaseLevDeque:
  buffer: circular array of Task   // grows when full
  head: atomic integer             // owner's end
  tail: atomic integer             // thieves' end

push(task):                 // owner only
  h = load(head)
  t = load(tail)
  if h - t >= size(buffer):
    buffer = grow(buffer)   // copy into a larger array
  buffer[h] = task
  store(head, h + 1)        // no CAS: only the owner writes head

pop():                      // owner only, newest task
  h = load(head) - 1
  store(head, h)            // claim the slot before looking at tail
  t = load(tail)
  if t > h:                 // deque was already empty
    store(head, h + 1)
    return null
  task = buffer[h]
  if t < h:                 // more than one task left: no thief can reach it
    return task
  // exactly one task left: race the thieves for it
  won = compare_and_swap(tail, t, t + 1)
  store(head, h + 1)        // deque is empty either way
  return task if won else null

steal():                    // any thief, oldest task
  t = load(tail)
  h = load(head)
  if t >= h:
    return null             // empty
  task = buffer[t]
  if compare_and_swap(tail, t, t + 1):
    return task
  return null               // lost to another thief or to the owner

The owner pays for a CAS only when one task is left. In every other pop it takes the task with plain loads and stores, because thieves move tail toward head one CAS at a time and cannot reach the slot the owner has just claimed while another task still sits between them.

The one-element case works like this. Suppose tail = 5 and head = 6, so slot 5 holds the only task. The owner lowers head to 5 and reads tail = 5. A thief that read tail = 5 and the old head = 6 also wants slot 5. Both try to move tail from 5 to 6 with a CAS. Exactly one CAS succeeds; the loser returns null. The owner then resets head to 6, which leaves the deque empty with head equal to tail.

The memory orderings are where real implementations get subtle. The owner’s store to head and its load of tail must not be reordered, and the same holds for the thief’s load of tail and load of head. Lê, Pop, Cohen and Zappa Nardelli (PPoPP 2013) gave a version for the C11 memory model that puts a sequentially consistent fence at both points and otherwise uses relaxed accesses, with a few acquire loads and one release fence. The atomics page covers CAS, acquire-release and sequential consistency.

Victim selection and idle management

Most implementations pick the victim at random. The randomized scheduler that Blumofe and Leiserson analyzed does exactly this, and Rayon picks a random starting worker and then tries the others in turn. If the steal fails (queue empty or another thief won), the thief tries a different victim. Random choice needs no shared state to decide the victim, so it adds no new bottleneck.

When every queue is empty, an idle worker can keep searching or go to sleep. Searching is cheap if the idle time is brief, and sleeping (parking the thread) is cheap if it is long. Most runtimes do both in sequence. A Rayon worker searches for work for a number of rounds, then becomes “sleepy”, then sleeps until new work is posted.

If steals become very common, their overhead can dominate. One response is to steal a block of tasks at a time instead of one, which amortizes the cost; Tokio and Go both steal half of the victim’s queue. Another is to chunk the work statically before parallel execution begins, accepting worse load balance in exchange for fewer steals.

9.1.6 When stealing wins

Work stealing excels when task sizes are irregular and the tree depth is unpredictable. Fork-join parallelism with recursive branching creates this shape naturally. One branch does little work. Another branch explodes into thousands of tasks. Stealing rebalances this without global synchronization.

Example: quicksort on randomly shuffled input. The two sides of a partition are rarely the same size, so the subtrees below them do unequal amounts of work. Stealing from the busier subtree keeps all cores busy.

Example: tree search. A tree node can have one child (a leaf) or hundreds (an internal node). Stealing from searches on deep branches keeps idle workers engaged.

Example: grouped data frame operations, if each group became a task. Some groups in a grouped operation are small. Others are large. Stealing from the large-group workers keeps small-group workers from idling.

9.1.7 When overhead dominates

Stealing is not free. Each steal is a few atomic operations, a CAS, and cache-line traffic. If task size is tiny (microseconds of work), the steal cost becomes significant.

Example: A task that adds two numbers takes a few nanoseconds. A steal needs atomic operations on a cache line that another core owns, which costs far more than that, so stealing such a task loses time compared with running it where it already is. The solution is task batching or chunking: group tiny tasks into batches and steal the batch, not the individual task.

Example: A task that allocates memory and runs a small computation. The allocation overhead dominates, and no scheduler can fix that. Profiling shows where the time goes: batch the allocations, or fuse several tasks into one.

For tiny, uniform work, a static schedule or chunking is simpler and faster than work stealing.

9.1.8 Task sizes in R

Tiny R functions

Calling an R function incurs interpreter overhead. Argument matching, environment setup, and the call stack all cost time. This overhead is fixed per call, not per instruction. If the function body does less work than the call itself costs, the interpreter dominates and the steal cost is a secondary concern. The solution is to batch: bundle many small function calls into one larger call, or vectorize.

Example: parallelizing lapply(1:1e6, function(x) x + 1) one element per task would create a million tasks of one addition each. The function call overhead dominates, and stealing does not help. Better: vectorize with 1:1e6 + 1, or batch into chunks.

R itself has no steal-based scheduler. parallel’s parLapplyLB() and mclapply(mc.preschedule = FALSE) balance load from the parent instead. parLapplyLB() sends the next chunk to whichever worker finishes first, and mclapply() without prescheduling forks a new job for the next element as soon as one exits. sunlight’s pmap() also dispatches from the parent. It cuts the input into about 16 strided chunks per worker by default, so that there are always pieces left to balance with, and sends the next chunk to whichever forked worker frees up.

Large numerical kernels

A numerical kernel runs for milliseconds or seconds: matrix multiplication, SVD, or a differential equation solver. Next to that, the scheduler’s own overhead, steals included, is negligible. What still matters is balance: whether the work comes in pieces small enough to share out evenly.

Example: You parallelize a large matrix multiply across four workers. Worker A gets a 1000 x 1000 block. Worker B gets a 100 x 100 block and finishes almost at once, then sits idle while A works. If A’s block were split so that B could steal half of it, the wall time would fall by nearly half, and the steal itself would cost nothing next to the kernel. For large kernels, how the work is cut matters and the cost of moving it does not.

parallel and future run R code in separate worker processes, forked (mclapply(), future’s multicore) or started fresh (PSOCK clusters, future’s multisession). A multithreaded BLAS inside a worker runs its own thread pool for each kernel call. For long numerical tasks the per-task scheduling cost in either layer is small next to the kernel.

9.1.9 Other systems

Cilk, from MIT in the mid-1990s, extended C with parallel function calls and ran them on a randomized work-stealing scheduler whose time and space bounds Blumofe and Leiserson proved. Cilk-5 (Frigo, Leiserson and Randall, PLDI 1998) built its runtime on the “work-first principle”: keep the cost of a spawn low, between 2 and 6 times a C function call in their measurements, and move scheduling overhead onto the steal path, which the theory says is taken rarely. Its deque used the THE protocol, under which a working victim takes a lock only when it and a thief go after the same task. That predates Chase-Lev by seven years. Intel’s Cilk Plus later carried the model into C and C++ compilers, and OpenCilk continues it today.

Rayon is a Rust library that brings work stealing to the Rust ecosystem. It uses a Chase-Lev deque per thread from the crossbeam-deque crate, picks victims from a random starting point, and lets idle threads search for a while before they sleep. Rayon is widely used for data-parallel code in Rust: the caller writes no explicit synchronization, and the scheduler handles load balance.

Tokio is a Rust async runtime. Its multi-thread scheduler gives each worker a local queue of at most 256 tasks and keeps one global queue, which takes half of a local queue when it overflows. A worker with nothing in either queue steals half of another worker’s local queue.

Go’s runtime uses work stealing for goroutine scheduling. It distinguishes G (a goroutine), M (an OS thread) and P (a processor, the resource an M must hold to run Go code). Each P has a local run queue of goroutines, and a P that runs out of work steals half of another P’s queue.

Java’s ForkJoinPool uses work stealing with a deque per worker. Its source describes the queue design as “roughly similar” to Chase-Lev’s. It is the standard tool for fork-join tasks in Java, and parallel streams run on its common pool.

9.1.10 Relevance to my work

sunlight

sunlight uses a forkserver and copy-on-write fork semantics to parallelize R. Each worker process starts as a copy of a preloaded template. pmap() chunks the work after the workers exist and dispatches the chunks on demand from the parent, as described above. Its NEWS file calls this work stealing; in the code there are no per-worker deques, only one queue of chunks in the parent that free workers draw from. If a worker runs compiled code (a numerical kernel), the library’s own thread pool can still use work stealing inside it, within the thread count the governor allows. The R-level overhead is amortized because each dispatch carries a chunk of iterations, not one task.

r-in-rust

r-in-rust is an R implementation written from scratch in Rust. Each of its interpreters is owned by one thread, so a work-stealing scheduler such as Rayon’s would distribute work across interpreters, not inside one. Because r-in-rust owns its runtime, the scheduler could be tightly integrated. The main question is task granularity: should each R function call be a task, or should groups of calls be batched into one task? The per-call overhead of the interpreter (argument matching, environment setup) suggests batching.

9.1.11 Tradeoffs and open questions

A future in-process sunlight scheduler (or a parallel-R scheduler in r-in-rust) must decide task granularity before stealing pays. If task size is set too small (one R function call per task), the steal overhead and interpreter overhead are comparable. If task size is too large (thousands of operations per task), load imbalance persists. The sweet spot is problem-dependent: data structure size, operation complexity, cache behavior. Profiling is mandatory.

How should R’s lazy evaluation interact with work stealing? If an R function receives an unevaluated promise, and the promise is forced during execution, does the task become two tasks (before and after the force)? If so, should stealing happen at promise boundaries?

Further reading

Chase, D., & Lev, Y. (2005). Dynamic Circular Work-Stealing Deque. In Proceedings of the seventeenth annual ACM symposium on Parallelism in algorithms and architectures (pp. 21-28). ACM. https://doi.org/10.1145/1073970.1073974

Blumofe, R. D., & Leiserson, C. E. (1999). Scheduling multithreaded computations by work stealing. Journal of the ACM, 46(5), 720-748. https://doi.org/10.1145/324133.324234

Frigo, M., Leiserson, C. E., & Randall, K. H. (1998). The implementation of the Cilk-5 multithreaded language. In Proceedings of the ACM SIGPLAN 1998 Conference on Programming Language Design and Implementation (pp. 212-223). The work-first principle and the THE protocol. https://doi.org/10.1145/277650.277725

Lê, N. M., Pop, A., Cohen, A., & Zappa Nardelli, F. (2013). Correct and efficient work-stealing for weak memory models. In Proceedings of the 18th ACM SIGPLAN Symposium on Principles and Practice of Parallel Programming (pp. 69-80). https://doi.org/10.1145/2442516.2442524

OpenCilk, the current Cilk implementation (the old cilk.mit.edu address redirects here). https://www.opencilk.org/

rayon documentation and source. https://docs.rs/rayon/

Tokio runtime documentation, including the multi-thread scheduler’s queues and stealing. https://docs.rs/tokio/latest/tokio/runtime/index.html

Go runtime scheduler, proc.go. https://go.dev/src/runtime/proc.go

Java ForkJoinPool documentation. https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html

Where this appears

Where to go next

Back to top