CAS Loop Pattern

Why do lock-free algorithms typically use compare-and-swap (CAS) in a retry loop rather than a single CAS attempt?

A

CAS loops are needed to work around the lack of hardware support for atomic operations on some platforms

B

A single CAS is not atomic, so multiple CAS calls are needed to form a complete atomic operation

C

The CAS may fail if another thread modified the shared state between the read and the CAS attempt, so the thread must reload the current value and retry

D

The CPU requires multiple CAS instructions to flush the store buffer