Skip to content

Instantly share code, notes, and snippets.

@fracasula fracasula/cond.go
Created Aug 3, 2018

What would you like to do?
GoLang: How to use sync.Cond
package main
import (
func main() {
lock := sync.Mutex{}
cond := sync.NewCond(&lock)
waitGroup := sync.WaitGroup{}
go func() {
defer waitGroup.Done()
fmt.Println("First go routine has started and waits for 1 second before broadcasting condition")
time.Sleep(1 * time.Second)
fmt.Println("First go routine broadcasts condition")
go func() {
defer waitGroup.Done()
fmt.Println("Second go routine has started and is waiting on condition")
fmt.Println("Second go routine unlocked by condition broadcast")
fmt.Println("Main go routine starts waiting")
fmt.Println("Main go routine ends")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.