liveBook · Manning

Spinlock

A spinlock is a mutual-exclusion synchronization primitive in which a waiting thread repeatedly checks or attempts to acquire the lock rather than blocking.

How 2 Manning books explain it

C++ Concurrency in Action, Second Edition

A mutex implementation in which a waiting thread repeatedly attempts to acquire the lock rather than blocking.

How it is used
Uses repeated atomic-flag attempts to implement mutual exclusion without a blocking call.
How deep it goes
Works through lock and unlock operations and their acquire-release ordering; warns about contention rather than developing a contention-handling strategy.
Examples
Shows a std::atomic_flag mutex and a barrier that yields while waiting for its generation to change.
What you can do after
Build it from scratch with std::atomic_flag and evaluate when busy-waiting or lock dependence makes it unsuitable.

Read how C++ Concurrency in Action, Second Edition explains spinlock →

Learn Concurrent Programming with Go

A spin lock is a mutual-exclusion lock whose unsuccessful acquisition repeatedly retries while the execution remains active instead of blocking.

How it is used
Protects grouped shared-state updates, such as multi-flight reservations, when single-variable atomic updates are insufficient.
How deep it goes
Works through a complete Go Locker implementation using an atomic integer, CompareAndSwap loop, scheduler yield, and atomic unlock.
Examples
Uses a multi-flight booking race and a Go SpinLock implementation with a 0/1 state flag.
What you can do after
Build it from scratch as a Go sync.Locker and extend it with TryLock or a spinning semaphore.

Read how Learn Concurrent Programming with Go explains spinlock →

Also covered in

What to know first

Related concepts

Questions about spinlock

What is spinlock?

A spinlock is a mutual-exclusion synchronization primitive in which a waiting thread repeatedly checks or attempts to acquire the lock rather than blocking.

Which Manning books explain spinlock?

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

What should I know before learning spinlock?

Manning books that teach spinlock usually introduce atomicity, deadlock and locking first.

What is the difference between spinlock and lock-free?

A spinlock is a mutual-exclusion synchronization primitive in which a waiting thread repeatedly checks or attempts to acquire the lock rather than blocking. Lock-free is a concurrency progress guarantee under which at least one operation completes within a finite number of steps despite contention, without relying on mutual-exclusion locks.

What is spinlock a kind of?

Spinlock is a kind of mutex. A mutex is a synchronization primitive that provides controlled exclusive access to shared data or a protected operation, admitting only one thread at a time.

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.