Skip to content

Instantly share code, notes, and snippets.

@Petelin Petelin/new_condition.go
Last active Mar 22, 2019

Embed
What would you like to do?
package main
import (
"fmt"
"sync"
"time"
)
func main() {
s := []string{"A", "B", "C"}
var conds = make(map[string]*sync.Cond, len(s))
for _, item := range s {
conds[item] = sync.NewCond(new(sync.Mutex))
}
wg := sync.WaitGroup{}
for i, item := range s {
wg.Add(1)
go func(index int, x string) {
wg.Done()
c.L.Lock()
for i := 0; i < 10; i++ {
c := conds[x]
c.Wait()
fmt.Println(i, x)
conds[s[(index+1)%3]].Signal()
}
c.L.Unlock()
}(i, item)
}
wg.Wait()
conds["A"].Signal()
time.Sleep(time.Hour)
}
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.