liveBook · Manning

Compare-and-exchange operation

A compare-and-exchange operation atomically 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-exchange operation →

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-exchange operation →

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-exchange operation →

Related concepts

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.