Locks are tricky — let's understand them by building one
summary
Explains why simple lock/lock keyword fails, demonstrates C# primitives (mutex, semaphore, Monitor) and lock-free Compare-and-swap loops, and shows a working memory-mapped file mutex that stores PID/TID, spins with timeouts and can kill stalled owners; runtime demos expose double-flush/finalizer corruption when Environment.Exit races with using, and deadlocks from killing threads or fork without exec. Recommends always acquiring locks with timeouts, avoid semaphores, refresh cloud leases, and cautions that async/await breaks traditional lock ownership; the custom mutex runs in production.
AI-written summary · about
chapters (24)
- 0:00Start
- 2:35General Concepts
- 3:58Parallelism
- 4:44Concurrent Parallel
- 7:00Shared Memory versus Message Passing
- 10:24Word Tearing
- 12:05Critical Section
- 19:48How Should We Tag a Concurrency Primitive
- 22:25Monitor
- 23:08Monitor with Condition Variable
- 28:14Problems with Locks
- 29:18Always Acquire Your Locks with Timeouts
- 31:22Compare and Swap Operation
- 38:20Double Check Lock
- 41:32Dll Main Lock
- 43:50Isolation Level
- 44:22Isolation Levels
- 47:39Deckers Algorithm
- 50:00Caches and Memory Model
- 53:08Problem with Async
- 55:36Mutex
- 55:56Acquire a Lock with a Timeout
- 1:04:04Avoid Semaphores
- 1:04:58Resources
related talks
01h 30mLive Lock-Free or Deadlock (Practical Lock-free Programming)
39mLive Lock-Free or Deadlock (Practical Lock-free Programming)
26mConcurrency: Unlocking a Solution
53mAtomics, Locks, and Tasks (part 1 of 2)
40mKernel Locking Engineering
26mTackling Contention: The Monsters Inside the 'sync.Locker'
52mSTM and Structured Concurrency — The Battle with Multithreading Dragons
42mReader-Writer Lock versus Mutex - Understanding a Lost Bet
