liveBook · Manning

Work stealing

As explained in Learn Concurrent Programming with Go

Work stealing is a scheduling technique in which an execution worker takes tasks from another worker's queue to improve utilization.

Work stealing is a scheduling technique that keeps processors busy by moving goroutines between local run queues. When one queue becomes empty, its kernel-level thread takes work from another queue instead of remaining idle. section 2.3.2 section 2.6

Go’s runtime uses work stealing in two closely related situations:

  • A goroutine makes a blocking call, such as waiting for I/O. The runtime moves its queue to a new or reused kernel-level thread so other goroutines can continue. section 2.3.2
  • A local run queue becomes empty while another queue still contains goroutines. The idle thread steals work from the busy queue, improving processor utilization. section 2.3.2
  • The technique supports parallel execution because Go places goroutines across multiple kernel-level threads, which can run on multiple processors. section 2.6

A blocked worker and a replacement

Imagine two kernel-level threads executing goroutines from separate local run queues. If a goroutine on thread B waits for I/O, the runtime creates or reuses thread C and moves work so the remaining goroutines keep running. Thread C can then steal tasks from the affected queue rather than waiting for the I/O operation to finish. section 2.3.2

Figure 2.10 (a) Kernel-level threads A and B are executing goroutines from their respective LRQs; (b) a goroutine is waiting on I/O blocking thread B, resulting in the creation or reuse of a new thread C, stealing work from the previous thread.

Shows local run queues before and after I/O blocks a thread, including the creation or reuse of a thread that steals work. section 2.3.2

How Learn Concurrent Programming with Go covers it

How deep it goes
Explains queue migration and replacement threads, but stops short of runtime implementation details or stealing policies.
Examples
Uses local run queues, I/O blocking, replacement thread creation, and an empty queue stealing from a busy queue.
What it assumes
Assumes basic awareness of goroutines and threads while distinguishing user-level, kernel-level, concurrent, and parallel execution.
What you can do after
Evaluate how Go maintains goroutine progress and processor utilization during blocking operations and uneven workloads.
How it connects
Connects goroutines with user-level threads, kernel-level threads, blocking calls, concurrency, and parallelism.

Also explained in

Compare how 5 books explain work stealing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Concurrent Programming with Go. The section links point to the parts of the book it draws on.