Roadmap › How Computers Work — CPU, Memory, OS, Processes and Threads › Module: Operating System, Processes and Threads
Lessons in this course (11)
9. Concurrency problems — race conditions, deadlocks and how to avoid them
Intermediate★ Must knowLesson 9 of 11
Concurrency = several tasks in progress at once (may interleave on one core). Parallelism = tasks literally running at the same time on several cores.
| Problem | What happens | Fix |
|---|---|---|
| Race condition | Two threads read-modify-write shared data; updates get lost | Locks, atomic operations (Interlocked), immutable data, don't share |
| Deadlock | Thread A holds lock 1 waiting for lock 2; B holds 2 waiting for 1 — both wait forever | Always lock in the same order, use timeouts, keep locks short |
| Starvation | A thread never gets the resource | Fair locks, priorities |
| Livelock | Threads keep reacting to each other without progress | Back-off with randomness |
| Thread-pool starvation | All pool threads blocked (e.g. .Result on async) | Async all the way |
csharp
int counter = 0;
Parallel.For(0, 1_000_000, _ => counter++); // ❌ race: result is less than 1,000,000
Console.WriteLine(counter);
int safe = 0;
Parallel.For(0, 1_000_000, _ => Interlocked.Increment(ref safe)); // ✅ atomic
var gate = new object();
lock (gate) { /* only one thread at a time here */ } // ✅ mutual exclusionThe same ideas appear in databases: SQL Server uses locks and isolation levels to stop concurrent transactions corrupting data, and can deadlock too (it then kills one transaction as the victim).