CPP Concurrency & Multithreading 2 — Questions and Answers
Question 1: What is thread starvation?
- A thread that consumes all available CPU
- A thread that is perpetually denied CPU time because higher-priority threads always preempt it (Correct answer)
- A thread waiting on a deadlocked mutex
- Running too many threads for the available cores
Correct answer: A thread that is perpetually denied CPU time because higher-priority threads always preempt it
Thread starvation happens when a thread never gets scheduled because other threads or high-priority tasks continuously consume CPU time.
Question 2: What is an atomic operation in concurrent programming?
- An operation that uses chemical principles
- An operation that completes entirely without interruption, appearing instantaneous to other threads (Correct answer)
- An operation that runs on a dedicated CPU core
- An operation performed inside a synchronized block
Correct answer: An operation that completes entirely without interruption, appearing instantaneous to other threads
Atomic operations are indivisible from other threads' perspectives, preventing partial updates that could cause race conditions.
Question 3: What is the producer-consumer pattern?
- A UI pattern separating data creation from display
- A concurrency pattern where producers add work to a shared queue and consumers process items from it (Correct answer)
- A memory pattern alternating allocation and deallocation
- A network pattern for request-response communication
Correct answer: A concurrency pattern where producers add work to a shared queue and consumers process items from it
The producer-consumer pattern decouples work generation from processing using a thread-safe buffer, enabling independent scaling of each side.
Question 4: What is thread-local storage (TLS)?
- Shared memory accessible only from threads within the same process
- A mechanism providing each thread with its own private copy of a variable (Correct answer)
- A fast per-thread heap allocator
- Storage for thread stack frames
Correct answer: A mechanism providing each thread with its own private copy of a variable
Thread-local storage gives each thread its own instance of a variable, eliminating the need for synchronization on that data.
Question 5: What is lock-free programming?
- Programming without any shared state between threads
- Designing concurrent algorithms that use atomic CPU operations instead of mutexes to avoid blocking threads (Correct answer)
- Using a single global lock for all shared resources
- Running threads in a single-threaded event loop
Correct answer: Designing concurrent algorithms that use atomic CPU operations instead of mutexes to avoid blocking threads
Lock-free algorithms use atomic operations (like CAS) to coordinate threads without ever blocking, avoiding deadlock and reducing contention.
Question 6: What is a condition variable used for in threading?
- Storing per-thread configuration flags
- Allowing threads to block and wait until a specific condition becomes true, then be notified to wake up (Correct answer)
- Checking CPU load before creating new threads
- Synchronizing file I/O across threads
Correct answer: Allowing threads to block and wait until a specific condition becomes true, then be notified to wake up
Condition variables work with a mutex to efficiently block a thread until another thread signals that a condition it needs has changed.
What is thread starvation?