CPP Concurrency & Multithreading 1 — Questions and Answers
Question 1: What is a race condition in concurrent programming?
- Two threads competing to be scheduled first at startup
- A bug where program outcome depends on the relative timing of thread execution (Correct answer)
- A deadlock involving more than two threads
- A thread running faster than the scheduler allows
Correct 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.
Question 2: What is a mutex (mutual exclusion lock)?
- A mechanism that allows all threads to read shared data simultaneously
- A synchronization primitive that allows only one thread at a time to access a critical section (Correct answer)
- A thread scheduling algorithm
- A lock-free data structure
Correct 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.
Question 3: What is a deadlock in multithreaded programs?
- A thread that is executing too slowly
- A state where two or more threads each wait forever for resources held by the others (Correct answer)
- A thread that never releases the CPU
- An exception thrown inside a thread
Correct 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.
Question 4: What is the difference between parallelism and concurrency?
- They are identical concepts
- Concurrency is about dealing with multiple tasks at once (possibly interleaved); parallelism is executing multiple tasks simultaneously on multiple processors (Correct answer)
- Parallelism is single-threaded; concurrency is multi-threaded
- Concurrency requires multiple CPUs; parallelism does not
Correct 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.
Question 5: What is a thread-safe data structure?
- A data structure that can only be used by one thread
- A data structure that correctly handles simultaneous access from multiple threads without data corruption (Correct answer)
- A data structure stored in thread-local storage
- A data structure that uses no locks
Correct 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.
Question 6: What is a semaphore in concurrent programming?
- A type of thread pool
- A signaling mechanism that controls access to a limited number of resources using a counter (Correct answer)
- A way to terminate threads gracefully
- A hardware interrupt handler
Correct 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.
What is a race condition in concurrent programming?