Implement a mutex without built-in locks

0
Remix

Build a lock with lock() and unlock() without using any lock, mutex, semaphore, monitor or condition variable the language or a library provides. Build it from atomic operations such as compare-and-swap (or a classic algorithm like Peterson's for two threads). Then answer fundamentals questions: what your lock relies on from the hardware and memory model, why spinning wastes CPU and how to back off or park threads, fairness, and what happens if a thread unlocks a lock it does not hold.

AppleCodingLast asked Aug 2026
No answer yet. Write the first one; the best answer gets endorsed.