Standard library / sync

sync.Cond

A condition variable lets goroutines wait for a predicate protected by L. Do not copy after first use. Expected stdout: 'ready\n'.

Formal Syntax & Structure

go syntaxgo
type Cond struct {
  L Locker
}

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")}
Go Reference