Concurrency & Multithreading Flashcards
6 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 6 Concurrency & Multithreading flashcards as text
What is a race condition in concurrent programming?
Answer: A bug where program outcome depends on the relative timing of thread execution
A race condition occurs when multiple threads access shared data concurrently and the result depends on their unpredictable execution order.
What is a mutex (mutual exclusion lock)?
Answer: A synchronization primitive that allows only one thread at a time to access a critical section
A mutex ensures mutual exclusion so that only one thread executes a critical section at a time, preventing race conditions.
What is a deadlock in multithreaded programs?
Answer: A state where two or more threads each wait forever for resources held by the others
Deadlock occurs when threads form a circular dependency on locks, so none can proceed because each waits for another to release.
What is the difference between parallelism and concurrency?
Answer: Concurrency is about dealing with multiple tasks at once (possibly interleaved); parallelism is executing multiple tasks simultaneously on multiple processors
Concurrency is a design concern about structuring a program to handle multiple tasks; parallelism is physical simultaneous execution on multiple cores.
What is a thread-safe data structure?
Answer: A data structure that correctly handles simultaneous access from multiple threads without data corruption
Thread-safe data structures use synchronization internally so concurrent reads and writes do not corrupt their state.
What is a semaphore in concurrent programming?
Answer: A signaling mechanism that controls access to a limited number of resources using a counter
A semaphore maintains a count representing available resource slots; threads wait when the count is zero and signal to increment it when done.