← Lessons

quiz vs the machine

Gold1360

Concurrency

The Cooperative Scheduler

Tasks yield voluntarily instead of being preempted by a timer.

4 min read · core · beat Gold to climb

The Cooperative Scheduler

A cooperative scheduler runs many tasks on a thread but switches between them only when a task voluntarily yields. This contrasts with preemptive scheduling, where a timer interrupt can pause any task at any instant.

In the cooperative model a task runs until it reaches a yield point, typically an await or an explicit yield call. At that moment it hands control back to the scheduler, which picks the next ready task. Because switches happen only at known points, there is no need to lock shared data against a switch in the middle of a small update.

  • Cheap switches No timer interrupt and no full register save, just a jump between saved task states.
  • Predictable A task is never interrupted mid statement, simplifying reasoning about shared data on one thread.
  • Fragile One task that never yields, for example a tight CPU loop, starves every other task on that thread.

The golden rule is to never block. A long computation or a synchronous IO call inside a cooperative task freezes the whole thread, so such work must be offloaded or broken into yielding chunks.

Key idea

A cooperative scheduler switches tasks only at voluntary yield points, which is cheap and predictable but freezes if any task refuses to yield.

Check yourself

Answer to earn rating on the learn ladder.

1. When does a cooperative scheduler switch tasks?

2. What is the main danger of cooperative scheduling?