Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement