Free preview

Requirements checklist

  • Primitives available: atomic exchange / CAS with ordering control; multi-core target.
  • Bounded critical sections assumed — ask, because it decides whether spinning is sane at all.
  • Correctness bar: mutual exclusion AND the memory-ordering contract, not just the flag.

The model

state: one word, 0 = free, 1 = held

lock:    while exchange(&s, 1, ACQUIRE) == 1:
             while load(&s, RELAXED) == 1: cpu_relax()   // TTAS: spin on a READ
             backoff()
unlock:  store(&s, 0, RELEASE)

The two arguments (know them cold)

  • Ordering: acquire on lock / release on unlock is what makes the critical section's memory operations visible to the next holder — without them you built a flag, not a lock.
  • Coherence: spinning on the atomic RMW ping-pongs the cache line in exclusive mode across every waiter; TTAS spins on a shared read-only copy and only attempts the RMW when the line changes. Same loop shape, order-of-magnitude less bus traffic.

Judgment: when a spinlock at all

  • Right: short critical sections, no preemption in the window, contention low.
  • Wrong: anything that can block or fault while holding it; long sections (burned cores); oversubscribed schedulers (the preemption pathology — the holder is descheduled and waiters spin at full speed on nothing).
  • The shipping compromise: spin-then-park hybrids — spin briefly, then sleep.

Complexity facts

  • Uncontended lock/unlock: O(1), a handful of cycles.
  • Contended TAS: coherence traffic grows with waiters per handoff; TTAS collapses it to one invalidation wave per release.
  • Backoff bounds the stampede at handoff; cpu_relax is the polite spin.

The five report dimensions, for this problem

  • Requirements & interface — asked about primitives, cores, and critical-section bounds before writing the loop.
  • Core design & invariants — the ordering contract stated as the definition of lock-ness; TTAS justified by the coherence argument, not folklore.
  • Extension probe — name the property under attack, trace the concrete schedule, cost the fixes.
  • Complexity honesty — traffic arguments in words, measurements before beliefs.
  • Communication — "raises the ceiling vs removes the risk" precision under pressing.

Ready? Sit the live mock → — the interviewer will run a twist this chapter deliberately hasn't shown you.

Enjoying the preview?

Create a free account to unlock the rest of this course, the in-browser judge, and live AI mock interviews.

Sign up free to continue