Thread safety, reentrancy and atomic updates

See two calls overwrite one shared buffer, a compare-and-swap fail and retry, and a test-and-set hand a lock to one thread. Threads and concurrency contracts · Operating systems.

Two threads call the same helper at the same moment, and one gets the other's answer. The same instant, two threads raise one counter, and a lock flag decides who goes first. Find which guarantee belongs to the function and which to the instruction.

What you will learn

  • Separate reentrant from thread-safe, and say why calls that share one buffer overwrite each other
  • Trace a compare-and-swap that fails, reloads and retries
  • Explain how test-and-set hands a lock to one thread, and what spinning costs

Lesson outline

  1. A helper that mixes up two callers
  2. Memory each call owns
  3. One shared number, one indivisible swap
  4. A lock built from one instruction

17 minutes · 4 scenes · 0 predict checks · 11 quiz questions

The full lesson is available with Premium.