-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
58 lines (53 loc) · 1.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"flag"
"fmt"
"runtime"
"time"
)
var nSeconds = flag.Int64("nSeconds", 20, "How many seconds will players play")
func main() {
flag.Parse()
t := runtime.GOMAXPROCS(16)
//nSeconds := 20
fmt.Printf("%v threads are used\n", t)
ball := make(chan struct{})
done := make(chan struct{})
n := 0
/*ticker := time.NewTicker(1 * time.Second)
<-ticker.C // Получение из канала ticker
ticker.Stop() // Остановка go-подпрограммы ticker*/
i := 2
for i > 0 {
var curi = i
go func(int) {
fmt.Printf("%v-player starts\n", curi)
for {
select {
case _, opened := <-ball:
if opened {
n++
ball <- struct{}{}
} else {
fmt.Printf("%v-player is done\n", curi)
return
}
case <-done:
fmt.Printf("%v-player is done\n", curi)
return
//default:
// fmt.Printf("%v-player is done\n", curi)
// return
}
}
}(curi)
i--
}
ball <- struct{}{}
time.Sleep(time.Duration(*nSeconds) * time.Second)
<-ball
close(ball)
close(done)
fmt.Printf("%v givings per %v seconds was done\n", n, nSeconds)
fmt.Scanln()
}