Skip to content

Instantly share code, notes, and snippets.

@Petelin
Last active March 22, 2019 02:35
Show Gist options
  • Save Petelin/25ac1f93aaa5d9605bfdfccbeb729079 to your computer and use it in GitHub Desktop.
Save Petelin/25ac1f93aaa5d9605bfdfccbeb729079 to your computer and use it in GitHub Desktop.
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