Why it matters

Locks serialize contended sections. Atomics can avoid this for simple operations, enabling much higher throughput on contended workloads.

Advertisement

The architecture

Atomic types: AtomicInteger, AtomicReference (Java); std::atomic (C++). Provide operations that complete indivisibly.

CAS: compare-and-swap. Atomically update if current value matches expected. Foundation of lock-free algorithms.

Atomic opsAtomic incrementsingle instructionCAScompare + swapLock-free algosbuilt on CASHardware provides CAS instruction; libraries expose language-level API
Atomic primitives.
Advertisement

How it works end to end

Memory ordering: acquire, release, sequentially consistent. Controls visibility of other memory operations relative to the atomic. Understand or use sequential consistency (default, safe).

ABA problem: value went A → B → A between reads; CAS thinks no change happened. Solve with versioning or hazard pointers.

Lock-free data structures: queues, stacks, hash maps. Complex but scale better under contention.