liveBook · Manning

Compare-and-swap

Also called CAS

Compare-and-swap is an atomic operation that replaces a value with a desired value only if the current value matches an expected value.

How 3 Manning books explain it

Learn Concurrent Programming with Go

Compare-and-swap is an atomic operation that compares a memory location with an expected value and replaces it with a new value only when they match, returning whether the replacement occurred.

How it is used
Uses compare-and-exchange to acquire lock flags and semaphore permits without an interruptible check-then-update gap.
How deep it goes
Defines success and failure semantics, then works through spin-lock and semaphore acquisition paths without detailing full runtime edge cases.
Examples
Demonstrates integer swaps from 17 to 19, a 0/1 spin-lock flag, and semaphore permit acquisition pseudocode.
What you can do after
Build it from scratch as a compare-and-exchange-based spin lock and choose waiting mechanisms when contention makes spinning wasteful.

Read how Learn Concurrent Programming with Go explains compare-and-swap →

C++ Concurrency in Action, Second Edition

An operation that atomically compares an object's value with an expected value and conditionally replaces it with a desired value.

How it is used
Prevents a concurrent stack push from overwriting another thread’s head update by making replacement conditional.
How deep it goes
Defines success, failure, expected-value updates, and spurious weak failures, then works through a retrying stack push; leaves node reclamation unresolved.
Examples
Uses an atomic boolean retry loop and a C++ lock-free stack push that updates an atomic head pointer.
What you can do after
Build it from scratch: write a retrying C++ stack push using compare_exchange_weak() while preparing the node before publishing it.

Read how C++ Concurrency in Action, Second Edition explains compare-and-swap →

Latency

A compare-and-swap instruction atomically compares a memory value with an expected value and conditionally replaces it with a new value.

How it is used
Supports synchronization algorithms and atomic pointer updates in concurrent linked-list insertion.
How deep it goes
Defines conditional atomic replacement and notes failed linked-list updates require a restart or error, without detailing an implementation workflow.
Examples
Illustrates atomically updating a linked-list pointer during insertion and identifies restart or error as failure responses.
What you can do after
Understand how conditional atomic updates support synchronization and where failed pointer updates constrain wait-free guarantees.

Read how Latency explains compare-and-swap →

Also covered in

What to know first

Related concepts

Questions about compare-and-swap

What is compare-and-swap?

Compare-and-swap is an atomic operation that replaces a value with a desired value only if the current value matches an expected value.

Which Manning books explain compare-and-swap?

Learn Concurrent Programming with Go, C++ Concurrency in Action, Second Edition and Latency.

What should I know before learning compare-and-swap?

Manning books that teach compare-and-swap usually introduce thread, garbage collection, pointer and queue first.

What is the difference between compare-and-swap and compare_exchange_strong()?

Compare-and-swap is an atomic operation that replaces a value with a desired value only if the current value matches an expected value. A C++ atomic member function that conditionally replaces a value and fails only when the comparison does not match.

What is compare-and-swap a kind of?

Compare-and-swap is a kind of atomicity. Atomicity is the property that an operation or group of operations takes effect as an indivisible whole, without observable partial effects.

What is compare-and-swap also called?

Compare-and-swap is also called CAS.

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.