liveBook · Manning

Wait

As explained in 100 Go Mistakes and How to Avoid Them

Wait is a sync.Cond method that releases its associated locker, blocks until notification, and reacquires the locker before returning.

A common mistake is replacing sync.Cond.Wait with a busy loop or a shared channel. Wait releases the associated locker, blocks until notification, then reacquires the locker before returning; the condition must still be checked in a loop. section 9.12

Waiting for donation goals

Each listener locks the condition, waits while donation.balance is below its goal, then prints after Wait returns. The updater increments balance while locked and calls Broadcast so all eligible listeners can recheck their predicates.

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Explains locker release, blocking, reacquisition, predicate loops, and Broadcast through runnable synchronization code.
Examples
Uses donation balances, listener goroutines, goal predicates, and updater notifications to demonstrate waiting.
What it assumes
Assumes familiarity with goroutines, mutex locking, shared state, and channel signaling.
What you can do after
Apply it via Go's sync.Cond to replace busy polling when goroutines await shared-state changes.
How it connects
Connects sync.Cond.Wait with mutexes, Broadcast, channels, goroutines, and shared-state predicates.

Also explained in

Compare how 4 books explain wait →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Go Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.