Questions: Atomic Operations and Compare-and-Swap

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

Thread T1 reads a shared pointer P = A, then gets descheduled. Thread T2 changes P: A → B → A. T1 resumes and executes CAS(P, A, new_value). What happens and why is it a problem?

AThe CAS fails because P was modified while T1 was descheduled
BThe CAS succeeds, but the underlying state may have changed in ways T1 did not account for
CThe CAS succeeds and is safe because P currently equals T1's expected value
DThe CAS fails because T2 performed two modifications rather than one
Question 2 Multiple Choice

A high-frequency trading system uses a shared reference counter incremented and decremented by many threads. Which synchronization approach is most appropriate?

AA mutex protecting all increment/decrement operations
BAn atomic CAS-based counter, since the critical section is tiny and mutex overhead would dominate
CNo synchronization, since increments on separate threads are independent
DA read-write lock, since increments and decrements are symmetric operations
Question 3 True / False

Unlike mutex acquisition, compare-and-swap instructions can be executed from user space without requiring a transition to kernel mode.

TTrue
FFalse
Question 4 True / False

Lock-free algorithms using CAS seldom cause threads to wait, so they are generally faster than mutex-based algorithms for the same operation.

TTrue
FFalse
Question 5 Short Answer

Explain why ordinary loads and stores cannot be composed into a correct compare-and-swap, even with careful ordering.

Think about your answer, then reveal below.