Concurrency & Synchronization
Find race conditions and make shared-memory code correct with locks, condition variables, semaphores and atomics.
4 objectives 6 steps about 135 min
Check what you know
The lessons are coming later. The knowledge check is ready now.
This skill page is a public outline. Its lessons and knowledge check appear here as subject-matter experts approve them.
Learning objectives
- Identify race conditions and critical sections in multithreaded code and explain why they produce nondeterministic results.
- Protect shared state with mutexes and atomic operations, and explain how locks are built from hardware atomics and futexes.
- Implement producer-consumer and reader-writer coordination with condition variables or semaphores.
- Detect data races with ThreadSanitizer and reason about the cost of lock contention and lock granularity.
Lesson plan
6 steps, about 135 min · 0 of 5 lessons available so far
- Why counter++ breaks: interleavings, critical sections and data races Concept · 20 min · coming later
- Locks from the ground up: test-and-set, compare-and-swap, spinning and futexes Concept · 25 min · coming later
- Lab: fix a racy bank-transfer program with a mutex, then with atomics Lab · 25 min · coming later
- Condition variables and semaphores: the bounded buffer, step by step Worked example · 25 min · coming later
- Lab: hunt races with ThreadSanitizer and measure lock contention Lab · 25 min · coming later
- Check: spot the race, choose the primitive Knowledge check · 8 questions
Practise it
Implement a thread-safe bounded queue in C with pthread mutexes and condition variables, exercise its invariants with a multi-producer, multi-consumer stress test that runs clean under ThreadSanitizer, and report throughput for 1, 2, 4 and 8 threads. Add invariant assertions, spurious wakeups and shutdown while producers wait; explain the coverage limits of both stress testing and ThreadSanitizer.
Build it from scratch
Practice builds that exercise this skill by re-creating a real technology.
- Build your own Chat ServerIntermediate · 8–16 h
- Build your own BitTorrent ClientIntermediate · 12–24 h
- Build your own Web ServerIntermediate · 8–16 h
- Build your own RedisIntermediate · 15–30 h
References
Hard prerequisites
Unlocks
On these roadmaps
Progress and evidence for this skill are shared across all of them.