English
Topic 3. Race conditions, deadlock, and starvation; thread synchronization tools in C#
Goal: understand the causes of race conditions, deadlock, and thread starvation; learn to protect shared data using Interlocked, lock, and Lock and coordinate threads using Monitor condition variables, semaphores, ReaderWriterLockSlim, ManualResetEventSlim, CountdownEvent, and Barrier; master ways to prevent deadlock, diagnose hangs, and measure the cost of synchronization.
Lecture contents
- Race conditions and mutual exclusion — Shared state and race conditions · Mutual exclusion requirements · Atomic operations: the Interlocked class
- Locks and condition variables — The lock statement, Lock type, and Monitor class · Condition variables: Monitor.Wait, Pulse, PulseAll · Mutex, Semaphore, and SemaphoreSlim
- Spin locks and signaling primitives — ReaderWriterLockSlim, SpinLock, and SpinWait · Signaling primitives
- Deadlocks and the cost of synchronization — Deadlock · Starvation, livelock, priority inversion, and lock convoys · Classic synchronization problems · The cost of synchronization
- Examples and common mistakes — Program examples · Common mistakes