Multicore & Synchronization
Which describes the ABA problem that affects lock-free data structures built with CAS?
- ATwo threads A and B can only run alternately
- BWhen a value changes from A to B and back to A, CAS fails to notice the change and succeeds
- CTwo locks A and B are taken in opposite orders
Show answer and explanation
Answer: B. When a value changes from A to B and back to A, CAS fails to notice the change and succeeds
Suppose a linked list's head pointer is A. While one thread reads A and prepares a CAS, other threads remove A, do work involving B, and reinsert a node at the same address A. The CAS succeeds because the value is still A, but the list has already changed and its structure is corrupted. It is prevented by pairing the pointer with a modification counter compared together, or by using LR/SC.
- A. It is not about execution order.
- B. Correct. It is a weakness of CAS comparing values only.
- C. That causes deadlock.