← Practice
Concurrency in C++
This is the wedge. Almost every candidate can define a mutex; very few can say what `memory_order_relaxed` guarantees, why their atomic counter got slower with more threads, or what makes double-checked locking broken before C++11 and fine after it.
Every question here is gated behind Explain Mode. You cannot see the answer until you have written your own and it has passed — because in the interview, the answer you can recognise is worth nothing and the answer you can say out loud is worth everything.
7 questions
- When is an atomic not enough?Everyone can say "an atomic is lock-free and cheaper". The question is what a mutex gives you that a pile of atomics does not.mediumWrittenExplain to unlock#atomics#c++#concurrency15 min
- Why did my counter get slower with more threads?Eight threads, eight separate counters, no locks — and throughput falls off a cliff. This is the question that finds out whether you know what a cache line is.mediumWrittenExplain to unlock#c++#cache#concurrency20 min
- What does memory_order_relaxed actually guarantee?The question that separates people who have used atomics from people who understand them.hardWrittenExplain to unlock#atomics#c++#concurrency25 min
- Was double-checked locking ever actually broken?A famous bug, a famous fix, and a much simpler answer almost nobody gives.hardWrittenExplain to unlock#c++#concurrency#memory-model20 min
- Bounded producer-consumer queueCondition variables, and the two mistakes almost everyone makes: waiting without a predicate, and signalling with the lock held.mediumWrittenExplain to unlock#c++#concurrency#condition-variable25 min
- The loop that should be fast and is notSumming a matrix column-by-column instead of row-by-row. A test of whether you think about memory at all.mediumWrittenExplain to unlock#c++#cache#memory20 min
- The ABA problemYour compare-and-swap succeeded. That does not mean nothing happened.hardWrittenExplain to unlock#atomics#c++#concurrency25 min