Last active
March 22, 2019 02:35
-
-
Save Petelin/25ac1f93aaa5d9605bfdfccbeb729079 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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