One Thread,Many Tasks

A hands-on series for systems programmers

Build a concurrency runtime.

Understand async and coroutines inside out.

Build it one working piece at a time. Runnable examples, exercises, and language notes help you follow along from Go, Rust, Zig, Odin, or C.

Three books in development · By Peter Mbanugo

BAKERY PROGRAMONE THREAD
Bread taskWaiting

Waiting for the simulated oven.

Next step: shelve the bread.
This stays in the task’s state.

Scheduler

Gives another ready task a turn.

Order taskRunning

The same thread takes an order.

Single thread with a multi-tasking runtime.

Start with something familiar

While one job waits, another can run.

The program has bread to bake and orders to take. Waiting for the oven doesn’t have to hold up the orders. The question is how the program lets go of one task to continue with another, and knows when and where to continue with the former.

  1. Remember the next step.

    The bread task puts a loaf in the oven. It keeps “shelve the bread” in its state and returns a wait request.

  2. Give other work a turn.

    The scheduler takes back control. The same thread can now run the order task while the baking works on its non-blocking task.

  3. Pick up where it left off.

    When the non-blocking baking ends, the bread task becomes ready. On its next turn, it reads its state and shelves the loaf.

See the handoff

Close to stop. Open again to replay.A still view of the handoff.

Control returns to the scheduler while a task waits The execution marker starts at the bread task, moves through the scheduler to the order task, then returns through the scheduler to the bread task. The bread task keeps its Shelve phase throughout the wait. Simulated time advances from tick 5 to tick 9. The same sequence is explained in the three steps above. Bread task NEXT: .Shelve Waiting Ready Loaf shelved Scheduler One thread clock: t5 clock: t6 clock: t9 Order task READY WORK Taking an order Order recorded One execution marker. Task state outlives each call.

This is a program with a simulated clock. The oven models waiting; the thread runs one task step at a time.

A little look inside

See the idea in two minutes.

The bakery, the saved state, and the same jobs inside a program.

Read the transcript and visual explanation

You write async code. But when a task pauses, what lets it carry on later? I’m writing One Thread, Many Tasks to make those steps visible.

Picture one baker. Bread in the oven. A customer ready to place an order. The bread needs more time. The baker can take the order. The next step for the bread is simple: take it out, and put it on the shelf.

Now put those jobs in a program. We’ll model them on one thread, with a simulated clock. Here, a task does one piece of work, then returns. Its progress lives in a struct, so the next call can pick up where it left off.

The animation shows the bread task keeping its phase after a call returns. These two lines save the next step, then ask to wait: b.phase = .Shelve and return coop.sleep_for(BAKE_TICKS). The bake duration is four simulated ticks.

Control returns to the scheduler. The order task can run. When the timer is up, the bread task becomes ready. On its next turn, it continues at the saved step. The execution marker moves between calls while task data stays in place. At tick nine, the bread task shelves the loaf.

Across three books, you’ll build this runtime yourself, in Odin. First, make tasks take turns. Then add waiting and channels. Then coordinate the work, and bring it to a clean stop. You’ll predict what happens, run the code, and check the trace.

The film closes with an invitation to follow the books as I write and receive previews and launch news at learnconcurrency.dev.

One Thread, Many Tasks

What you’ll build in each book.

You’ll work on the same runtime through all three books. Each book picks up where the previous one left off. You keep the code you’ve written and add what the next program needs.

Book 1: Take Turns

Start with two jobs that need to share a thread. You’ll make them take turns, give each one a way to remember its progress, and build the scheduler that runs them.STATE · STEPS · SCHEDULING

Book 2: Wait Without Blocking

You’ll give tasks a way to wait and wake up while other work carries on. You will add timers, events, and channels, and see how a full queue can make a sender wait.WAITING · CHANNELS · BACKPRESSURE

Book 3: Tasks Working Together

You’ll bring the pieces together in larger programs, with futures, actors, and pipelines. The runtime becomes more capable to manage tasks lifetime, cancellation, and how to shut down cleanly.FUTURES · ACTORS · TASK LIFETIMES

Work through it yourself

Learn by running the code.

Each chapter gives you a small program to run and change. Before running it, you’ll work out which task will run next and what happens when another task has to wait.

Then you’ll run it and look at the log of what each task did. After that you'll compare the result with your prediction. If something surprises you, you can follow it back to the code.

The exercises ask you to add, change, or fix something. You’ll write tests to check that the program behaves as you intended.

Designed for programmers who learn by making.

The examples use Odin, with language notes and appendices for Go, Rust, Zig, and C readers. Familiarity with systems programming helps. You can start without knowing concurrency because the programs use one thread and simulated time so you can follow what happens.

Follow two tasks through the program.

  1. Tick 5

    Bread task
    Waits until tick 9.
    Order task
    Takes an order.
  2. Tick 6

    Bread task
    Still waiting.
    Order task
    Finishes the order.
  3. Tick 9

    Bread task
    Runs again and shelves the loaf.
    Order task
    Already finished.
The clock is simulated. While the bread task waits, the same thread runs the order task.

A note from Peter

Why I’m writing this.

I want to help you understand what your concurrency tools are doing, so you can make better decisions about how to use them.

When a task waits, you should be able to explain what it is waiting for, what can run instead, and how it will continue.

These books give you a small runtime where you can follow those steps yourself. You write the code, change its behaviour, and see what happens.

Follow the work as it takes shape

Get inside the books, early.

Selected draft chapters and exercises to try. Get News when the books are ready.