-
Notifications
You must be signed in to change notification settings - Fork 2
/
exercise1.go
181 lines (155 loc) · 5.38 KB
/
exercise1.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2014 Ardan Studios
//
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Fix the race condition in this program.
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
// numbers maintains a set of random numbers.
var numbers []int
// mutex will help protect the slice.
var mutex sync.Mutex
// init is called prior to main.
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
// Number of goroutines to use.
const grs = 3
// wg is used to manage concurrency.
var wg sync.WaitGroup
wg.Add(grs)
// Create three goroutines to generate random numbers.
for i := 0; i < grs; i++ {
go func() {
random(10)
wg.Done()
}()
}
// Wait for all the goroutines to finish.
wg.Wait()
// Display the set of random numbers.
for i, number := range numbers {
fmt.Println(i, number)
}
}
// random generates random numbers and stores them into a slice.
func random(amount int) {
// Generate as many random numbers as specified.
for i := 0; i < amount; i++ {
n := rand.Intn(100)
mutex.Lock()
{
numbers = append(numbers, n)
}
mutex.Unlock()
}
}
/*
==================
WARNING: DATA RACE
Read at 0x0000005f9200 by goroutine 9:
main.random()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:55 +0x92
main.main.func1()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:35 +0x37
Previous write at 0x0000005f9200 by goroutine 7:
main.random()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:55 +0xf6
main.main.func1()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:35 +0x37
Goroutine 9 (running) created at:
main.main()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:34 +0xbc
Goroutine 7 (finished) created at:
main.main()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:34 +0xbc
==================
==================
WARNING: DATA RACE
Read at 0x0000005f9200 by goroutine 8:
main.random()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:55 +0x92
main.main.func1()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:35 +0x37
Previous write at 0x0000005f9200 by goroutine 7:
main.random()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:55 +0xf6
main.main.func1()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:35 +0x37
Goroutine 8 (running) created at:
main.main()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:34 +0xbc
Goroutine 7 (finished) created at:
main.main()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:34 +0xbc
==================
==================
WARNING: DATA RACE
Read at 0x00c0000ac048 by goroutine 8:
runtime.growslice()
/usr/local/go/src/runtime/slice.go:76 +0x0
main.random()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:55 +0x168
main.main.func1()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:35 +0x37
Previous write at 0x00c0000ac048 by goroutine 7:
main.random()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:55 +0xd3
main.main.func1()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:35 +0x37
Goroutine 8 (running) created at:
main.main()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:34 +0xbc
Goroutine 7 (finished) created at:
main.main()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:34 +0xbc
==================
==================
WARNING: DATA RACE
Read at 0x00c0000ac048 by goroutine 9:
runtime.growslice()
/usr/local/go/src/runtime/slice.go:76 +0x0
main.random()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:55 +0x168
main.main.func1()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:35 +0x37
Previous write at 0x00c0000ac048 by goroutine 7:
main.random()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:55 +0xd3
main.main.func1()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:35 +0x37
Goroutine 9 (running) created at:
main.main()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:34 +0xbc
Goroutine 7 (finished) created at:
main.main()
/home/cedric/m/dev/work/repo/experiments/go/ultimate-go/concurrency/data_race/exercise1/exercise1.go:34 +0xbc
==================
0 79
1 1
2 1
3 60
4 67
5 91
6 5
7 86
8 46
9 52
10 51
11 39
12 78
13 21
14 94
15 59
16 72
17 96
18 11
19 35
Found 4 data race(s)
*/