Questions: Mutual Exclusion and Locks

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

Thread A and Thread B both execute the sequence: (1) read lock variable — see it is free, (2) set lock variable to 'taken'. What concurrency problem does this expose?

ABoth threads will enter the critical section simultaneously, violating mutual exclusion
BThread B will be permanently blocked waiting for Thread A to release the lock
CThe lock variable will overflow from concurrent writes
DThis is not a problem — only one thread can read memory at a time
Question 2 Multiple Choice

A system uses a blocking lock for a critical section that consistently completes in 3 nanoseconds on a modern CPU. A performance engineer proposes switching to a spinlock. What is the likely performance outcome?

AWorse — spinlocks waste CPU cycles that could serve other threads during longer critical sections
BBetter — for a very short critical section, avoiding two context switches (sleep + wake) outweighs the cost of briefly spinning
CIdentical — spinlocks and blocking locks have the same overhead by design
DWorse — spinlocks cannot guarantee mutual exclusion for nanosecond-scale operations
Question 3 True / False

A correctly implemented spinlock guarantees that nearly every thread waiting for the lock will eventually acquire it.

TTrue
FFalse
Question 4 True / False

The fundamental hardware requirement for any correct lock implementation is an operation that atomically reads and writes a shared variable in a single indivisible step.

TTrue
FFalse
Question 5 Short Answer

Why must the 'check if free' and 'mark as taken' steps of a lock's acquire() operation be atomic, and what goes wrong if they are not?

Think about your answer, then reveal below.