Standard library / sync
sync.Cond.Wait()
Release L and wait for a signal, then reacquire L. Call with L held, and always recheck the predicate in a loop. Expected stdout: 'ready\n'.
Formal Syntax & Structure
go syntaxgo
func (c *Cond) Wait()Working Example
Example Snippetgo
package main
import ( "fmt" "sync")
func main() { var mu sync.Mutex c := sync.NewCond(&mu) ready := false entered := make(chan struct{}) done := make(chan struct{}) go func() { mu.Lock() close(entered) for !ready { c.Wait() } mu.Unlock() close(done) }() <-entered mu.Lock() ready = true c.Signal() mu.Unlock() <-done fmt.Println("ready")}