liveBook · Manning

Sync.Cond

As explained in Learn Concurrent Programming with Go

The Go standard-library condition-variable type that coordinates waiting and signaling through a sync.Locker.

sync.Cond coordinates goroutines that share state through a sync.Locker. A waiting goroutine checks a condition while holding the lock, then calls Wait when the condition is false. Wait atomically releases the lock and suspends execution; returning from Wait reacquires it. section 5.1.1

Waiting for enough money

A spending goroutine locks cond.L and waits while money is below 50. A funding goroutine adds money, calls Signal, and unlocks. The spender resumes, reacquires the lock, and subtracts 50 without allowing the balance to become negative.

How Learn Concurrent Programming with Go covers it

How deep it goes
Explains the API and guarded wait-signal pattern, including atomic unlock-and-suspend behavior, but avoids implementation internals.
Examples
Uses Stingy and Spendy goroutines to coordinate deposits and spending through a shared bank balance.
What it assumes
Assumes familiarity with goroutines, mutex locking, shared variables, and critical sections.
What you can do after
Build it from scratch using sync.NewCond, Locker methods, Wait, Signal, and a mutex-protected condition loop.
How it connects
Ties condition variables to mutexes, goroutines, shared state, critical sections, and condition checking.

Also explained in

Compare how 2 books explain sync.Cond →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Concurrent Programming with Go. The section links point to the parts of the book it draws on.