Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 07 · How Computers Work · Build it concurrent · about 19 min to read

Play it as a journeyTutor comes with a plan

Who sees your write first: memory ordering

Concurrency and the Hardware
Concurrency and the Hardware · Open board

Two cores with their own caches and one shared memory. This lesson asks which core sees a write first, and what happens when two cores disagree about the order.

Before you start. A thread is one line of work in a program, and a core is one processor of the chip, running one thread at a time (One key, many hands). Each core keeps its own small copy of recent memory, called a cache, so one core can hold an older value than another (The Memory Hierarchy (cache, locality, the memory wall)). A lock lets one thread at a time into a stretch of code, and an atomic add updates one number in a single step nobody can interrupt (One key, many hands). The earlier lesson was about how fast these are. This one is about what a thread sees when there is no lock at all.

Lesson 41 of 41 in How Computers Work

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • The problem: every line is correct and the answer is still wrong
  • Six orders, and none gives 0 and 0
  • The run: both threads read 0
  • The store buffer: a write waits before others can see it
  • The memory barrier: empty the store buffer first
  • Release and acquire: order only what you need
  • The memory model: the rules a language promises
  • Why lock-free code is hard

Terms it explains: store buffer, memory barrier, release, acquire, memory model, actor / message passing, Amdahl's law, async / event loop, atomic, cache coherence (MESI), CAS, concurrency, coroutine/channel, critical section, deadlock, happens-before, memory barrier/fence, memory reordering, mutex/lock, mutual exclusion, parallelism, race condition, starvation / livelock.

Lab

Ninety-five percent of the job splits across cores. How much faster can it possibly finish?

Leave The part that can be split up at 95%, drag Cores you throw at it upward, and watch What the next core buys.

Open The parallel ceiling

Thirty-two hands share one key. The room is no slower than with two, so where did the extra hands go?

Stay on One key for the whole room, drag Hands that need the key from 2 to 32, and watch Time spent waiting.

Open The one key

Related papers

Review How Computers Work