Concurrency Support
The standard library gives C++ threads, mutexes, condition variables, atomics, futures and coroutines. Interviewers ask about data races, memory order and deadlock-free locking.
on this pageshowhide
explore
- Threads and Stop Tokens5 questions
- Mutex and Lock Types6 questions
- Condition Variables5 questions
- Atomics and Memory Order6 questions
- Futures and Async5 questions
- C++20 Coroutines6 questions
questions
page 2 of 2In C++, what do is_always_lock_free and is_lock_free tell you about a std::atomic<T>, and what happens when they are false?
basics
~20 sThey report whether operations on that atomic type avoid locks: is_always_lock_free at compile time, is_lock_free at run time. When false, the type is still fully atomic, but the library may use a lock, so operations can block.
In C++20, how does std::condition_variable_any::wait(lock, stop_token, pred) let a std::jthread parser stop while blocked, and what does its bool mean?
basics
~20 sA stop request on the token wakes the blocked thread, and std::jthread's destructor makes one. The overload returns pred(): false means it stopped with nothing to do; true means the predicate holds, even if a stop was requested.
In C++20, where does a coroutine's frame live, who frees it, and when can the compiler elide its heap allocation?
basics
~20 sThe frame, or coroutine state, comes from the promise type's operator new if it declares one, else the global one. It is freed when the coroutine runs off its end or destroy() is called, and may be elided when it cannot outlive the caller.
showing 31–33 of 33