← Lessons

quiz vs the machine

Gold1430

Concurrency

The Task Waker Mechanism

How a parked task gets back onto the ready queue exactly when its input arrives.

5 min read · core · beat Gold to climb

The signal that says wake me

When a future returns Pending, it parks. The waker is the handle that lets whatever it is waiting on put the task back on the executor's ready queue at the right moment.

How the handshake works

  • When the executor polls a task, it passes in a context carrying that task's waker.
  • The future stores the waker with the resource it is blocked on, for example a socket or a timer.
  • When the resource becomes ready, it calls wake on the stored waker.
  • Waking moves the task back onto the ready queue, and the executor re polls it.

Why this is efficient

The waker turns polling into an event driven loop. A task is polled once, then sleeps for free until its exact event fires. There is no spinning and no scanning of every pending task. Millions of tasks can wait on millions of resources, and only the ones whose events arrive get re polled.

Key idea

The waker is the callback that a parked task hands to the resource it waits on, so when the event fires the task is requeued and polled again with no busy spinning.

Check yourself

Answer to earn rating on the learn ladder.

1. What does a future do with the waker it receives?

2. What happens when wake is called?

3. Why does the waker mechanism avoid busy spinning?