Standard library / sync

sync.NewCond()

Create a condition variable using the supplied Locker. Expected stdout: 'ready\n'.

Formal Syntax & Structure

go syntaxgo
func NewCond(l Locker) *Cond

Parameters & Attributes

NameTypeDescription
lLockerLock protecting the condition predicate.

Return Value

*Cond

Condition variable associated with l.

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