skip to content

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 pageshow

explore

questions

page 2 of 2

In 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?

level: middleimportance: nice to knowfreq 30%

basics

~20 s

They 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.

open as a page

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?

level: seniorimportance: nice to knowfreq 26%

basics

~20 s

A 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.

open as a page

In C++20, where does a coroutine's frame live, who frees it, and when can the compiler elide its heap allocation?

level: seniorimportance: nice to knowfreq 30%

basics

~20 s

The 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.

open as a page

showing 31–33 of 33