Skip to main content
PRISM

Check-then-act

foundational · asked in almost every interview

`if (!map.has(k)) map.set(k, v)`, raced against itself. Both threads check, both find it missing, and both create it.

Enumerating the interleavings

What this shows

The window is between the check and the act. A lock that begins after the check protects nothing; the fix is to ask and act in one indivisible operation.

Why it is asked

The TOCTOU pattern behind a huge share of real bugs — duplicate charges, duplicate accounts, two connections where one was intended.

In an interview

The TOCTOU pattern behind a huge share of real bugs — duplicate charges, duplicate accounts, two connections where one was intended.

  • TOCTOU
  • atomicity
  • putIfAbsent
  • idempotency

Run these next

The rest of races and atomicity