Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 8d 09h 54m 22s8 days 9 hours 54 minutes 22 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 8d 09h 54m 22s8 days 9 hours 54 minutes 22 seconds.

Sign in to watch this lesson
Races and Locks · 4:09
We open up the mutex and build locks ourselves, judging each by correctness, fairness and cost. A plain flag lets two threads in because testing and setting are separate steps; test-and-set (atomic exchange) and compare-and-swap fix that with one instruction (swpa and casa on an Apple M3, or an ldaxr/stxr loop on older Arm), and a ticket lock built on fetch-and-add (ldadd) serves waiters strictly in order. Measured with eight threads: the flag lock kept barely a third of its increments, the exchange and CAS locks let one thread win hundreds or thousands of times in a row, and the ticket lock gave every thread an equal share.