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
Which C++ standard introduced `std::atomic` and the memory model?
Answer: C++11
C++11 introduced `std::atomic`, `std::thread`, and a formal memory model for concurrent programming.
What does `std::memory_order_acquire` guarantee when used in a load operation?
Answer: All subsequent reads/writes in the current thread see effects from the release store they synchronized with
`memory_order_acquire` ensures that subsequent reads and writes in the current thread cannot be reordered before this load, forming the acquire side of a release-acquire pair.
What is a spurious wakeup in the context of `std::condition_variable`?
Answer: A wakeup that occurs without any thread calling notify_one or notify_all
A spurious wakeup is when `wait()` returns without any thread having called `notify_one` or `notify_all`, which is why wait should always be used with a predicate.
What is the difference between `std::lock_guard` and `std::unique_lock`?
Answer: `std::unique_lock` supports deferred locking, manual unlock, and timed locking; `std::lock_guard` is a simpler RAII wrapper
`std::unique_lock` provides more flexibility including deferred locking, try-locking, timed locking, and manual unlock/relock, while `std::lock_guard` is a lightweight non-movable RAII wrapper.
What does `std::thread::detach()` do?
Answer: Allows the thread to run independently so its resources are freed automatically upon completion
`detach()` separates the thread of execution from the `std::thread` object, allowing the thread to run independently; its resources are reclaimed automatically when it finishes.
Which of the following is true about `std::recursive_mutex`?
Answer: It allows the same thread to lock it multiple times without deadlocking
`std::recursive_mutex` allows the same thread to acquire the lock multiple times; it must be unlocked the same number of times it was locked.
What happens if a `std::thread` object is destroyed while it is still joinable?
Answer: `std::terminate()` is called
If a `std::thread` object is destroyed while joinable (neither joined nor detached), `std::terminate()` is called, which typically aborts the program.