Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "sync"
  6. "time"
  7. )
  8.  
  9. func main() {
  10. s := []string{"A", "B", "C"}
  11. var conds = make(map[string]*sync.Cond, len(s))
  12. for _, item := range s {
  13. conds[item] = sync.NewCond(new(sync.Mutex))
  14. }
  15. wg := sync.WaitGroup{}
  16. for i, item := range s {
  17. wg.Add(1)
  18. go func(index int, x string) {
  19. wg.Done()
  20. c.L.Lock()
  21. for i := 0; i < 10; i++ {
  22. c := conds[x]
  23. c.Wait()
  24. fmt.Println(i, x)
  25. conds[s[(index+1)%3]].Signal()
  26. }
  27. c.L.Unlock()
  28. }(i, item)
  29. }
  30. wg.Wait()
  31. conds["A"].Signal()
  32. time.Sleep(time.Hour)
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement