DSA Factory
Free Synchronization lessonsSynchronization · Stage 1 · Locks and semaphores · Step 1

Locks and atomic instructions

See how a mutex fixes the counter, how hardware makes a lock possible, and when spinning beats sleeping. About 13 minutes.

Quick check

Why must the lock-acquire operation itself use an atomic instruction such as test-and-set?

  1. AOtherwise two threads could both see the lock as free and both take it
  2. BAtomic instructions are the fastest instructions
  3. CThe OS refuses to run non-atomic code
Show the answer

Otherwise two threads could both see the lock as free and both take it. Checking and setting the lock would be a race of its own.

Lock it in

4 questions on this idea

Short questions that apply the idea to new cases: trace it, work out a number, put steps in order. A wrong answer explains itself.

Answer them in your browserFree account, no card. Your progress is saved, and the idea comes back in a quick review later.
Sign up free to solve