Real cases

Two of the ideas you practise, meeting in one real situation. Choose, see what actually happens, then reflect.

A case · you decide

Trice and Baton lose an update

Trice & Baton — a 17-year-old debugging a like-counter in a club project

The idea in play: counter += 1 is three steps (read, add, write), and the order the tasks run in can lose one update.

Two tasks each run counter += 1 once, starting from 0. Sometimes the counter ends at 2, and sometimes at 1.

Trice: each += is read, add, then write.

Baton: the two tasks' reads and writes can interleave in any order.

What is the smallest final value possible?

Race two tasks →

A case · you decide

Sheara and Isla fix the counter

Isla & Sheara — a 16-year-old choosing a fix for the same race

The idea in play: shared, mutable state is where races live; an actor serialises access so only one task touches it at a time.

The counter is shared between two tasks and both change it. Isla's teammates suggest three fixes.

Sheara names the problem: the state is shared AND mutable.

Isla's tool is the actor: whatever lives inside it is touched by one task at a time, each change finishing before the next starts.

Which fix is correct?

Race two tasks →