Concurrency & Multithreading Flashcards
7 cards from real CPP practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 7 Concurrency & Multithreading flashcards as text
What is a deadlock, and which condition is NOT required for it to occur?
Answer: Preemption — resources can be forcibly taken away, which actually prevents deadlock
Deadlock requires mutual exclusion, hold-and-wait, no preemption, and circular wait; if preemption exists (resources can be forcibly reclaimed), deadlock cannot occur.
What does `std::shared_mutex` enable that `std::mutex` does not?
Answer: Multiple concurrent readers with exclusive writer access
`std::shared_mutex` supports shared (reader) locks via `lock_shared()` allowing multiple readers simultaneously, and exclusive (writer) locks via `lock()` for single-writer access.
What is the ABA problem in lock-free programming?
Answer: A thread reads value A, another changes it to B then back to A; the first thread's CAS succeeds even though the data structure changed
In the ABA problem, a CAS operation sees the expected value A and succeeds, but the state has changed from A→B→A in between, leading to incorrect behavior in pointer-based lock-free structures.
What is the output behavior of `std::cout` when used from multiple threads without synchronization in C++?
Answer: Individual calls to `operator<<` are not data races, but output may be interleaved between calls
Since C++11, individual calls to `operator<<` on `std::cout` are thread-safe (no data race), but outputs from chained calls like `cout << a << b` can be interleaved between threads.
What does `std::atomic::fetch_add()` return?
Answer: The value before the addition was applied
`fetch_add()` atomically adds to the stored value and returns the **previous** value that was stored before the addition.
Which launch policy for `std::async` guarantees execution in a new thread?
Answer: `std::launch::async`
`std::launch::async` forces `std::async` to launch the callable in a new thread, whereas `std::launch::deferred` runs it lazily in the calling thread when `.get()` is called.
What is thread starvation?
Answer: A thread that is perpetually denied access to a resource because higher-priority threads always acquire it first
Thread starvation occurs when a thread is continuously blocked from acquiring a resource because other threads — often higher-priority ones — repeatedly acquire it, leaving the starved thread unable to progress.