-
Notifications
You must be signed in to change notification settings - Fork 0
/
use_barrier.go
62 lines (55 loc) · 884 Bytes
/
use_barrier.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
59
60
61
62
package main
import (
. "barrier"
"fmt"
"sync"
)
type thrArg struct {
incr int
arr [narr]int
}
var (
thrs [nthr]thrArg
wg sync.WaitGroup
barrier = NewBarrier(nthr)
)
const (
outloops = 10
inloops = 1000
nthr = 5
narr = 6
)
func thrFunc(arg *thrArg) {
defer wg.Done()
for i := 0; i < outloops; i++ {
barrier.Wait()
for j := 0; j < inloops; j++ {
for k := 0; k < narr; k++ {
arg.arr[k] += arg.incr
}
}
if barrier.Wait() {
for i := 0; i < nthr; i++ {
thrs[i].incr += 1
}
}
}
}
func main() {
for i := 0; i < nthr; i++ {
thrs[i].incr = i
for j := 0; j < narr; j++ {
thrs[i].arr[j] = j + 1
}
wg.Add(1)
go thrFunc(&thrs[i])
}
wg.Wait()
for i := 0; i < nthr; i++ {
fmt.Printf("%02d: (%d) ", i, thrs[i].incr)
for j := 0; j < narr; j++ {
fmt.Printf("%010d ", thrs[i].arr[j])
}
fmt.Println()
}
}