← All CPP Flashcard Decks

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
  1. What is a data race in C++?

    Answer: Two threads simultaneously accessing the same memory location where at least one access is a write and neither is synchronized

    A data race is undefined behavior in C++ and occurs when two or more threads concurrently access the same memory location without synchronization, with at least one write.

  2. Which memory order provides the strongest synchronization guarantee in C++?

    Answer: `std::memory_order_seq_cst`

    `std::memory_order_seq_cst` (sequentially consistent) is the default and strongest ordering, ensuring a single total order of operations across all threads.

  3. What does `std::async` return?

    Answer: `std::future`

    `std::async` launches a callable asynchronously (or deferred) and returns a `std::future` object through which the result can be retrieved.

  4. What is the purpose of `std::promise` in C++ concurrency?

    Answer: To provide a one-time channel for passing a value or exception from a producer thread to a consumer via an associated `std::future`

    `std::promise` is one end of a future-promise pair: the promise sets a value or exception, and the associated `std::future` retrieves it, enabling one-time inter-thread communication.

  5. What is `std::call_once` used for?

    Answer: Ensuring a function executes exactly once across all threads, even with concurrent calls

    `std::call_once` with a `std::once_flag` guarantees that a callable is executed exactly once, even if multiple threads call it simultaneously — useful for thread-safe lazy initialization.

  6. What is false sharing in multithreaded programs?

    Answer: When two threads modify different variables that reside on the same cache line, causing unnecessary cache invalidation

    False sharing occurs when variables used by different threads share a cache line; writes by one thread invalidate the entire line for others, degrading performance.

  7. Which C++ feature enables lock-free programming by allowing atomic read-modify-write operations?

    Answer: `std::atomic::compare_exchange_strong()`

    `compare_exchange_strong()` atomically compares the stored value to an expected value and, if equal, replaces it with a desired value — the core operation for building lock-free data structures.