CAS loop
Read the value, work out the new one, try a compare-and-swap, and go round again if another thread changed it first.
A CAS loop implements an arbitrary atomic update f: load old, compute new = f(old), compare-and-swap(p, old, new), and retry with the freshly observed value on failure; it is lock-free but may retry repeatedly under contention.
See also