Created
March 16, 2016 03:56
-
-
Save lon9/3a1299fa670e3c708bdd to your computer and use it in GitHub Desktop.
ズンドコキヨシGolang並列版 ref: http://qiita.com/Rompei/items/bfa03fbc9a94a37703bb
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" | |
"math/rand" | |
"runtime" | |
"sync" | |
"time" | |
) | |
// Zundoko is ズンドコ配列 | |
var Zundoko = [2]string{"ズン", "ドコ"} | |
func main() { | |
cpus := runtime.NumCPU() | |
runtime.GOMAXPROCS(cpus) | |
kiyoshiCh := make(chan bool, 1) | |
var m sync.Mutex | |
for i := 0; i < cpus; i++ { | |
go zndk(kiyoshiCh, &m) | |
} | |
<-kiyoshiCh | |
close(kiyoshiCh) | |
fmt.Println("キ・ヨ・シ!") | |
} | |
func zndk(kiyoshiCh chan bool, m *sync.Mutex) { | |
zdk := make([]string, 5) | |
for { | |
for i := 0; i < 5; i++ { | |
zdk[i] = getZndk(m) | |
} | |
if zdk[0] == "ズン" && zdk[1] == "ズン" && zdk[2] == "ズン" && zdk[3] == "ズン" && zdk[4] == "ドコ" { | |
for _, s := range zdk { | |
fmt.Println(s) | |
} | |
kiyoshiCh <- true | |
break | |
} | |
} | |
} | |
func getZndk(m *sync.Mutex) string { | |
m.Lock() | |
defer m.Unlock() | |
rand.Seed(time.Now().UnixNano()) | |
return Zundoko[rand.Intn(2)] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment