forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example1.go
80 lines (62 loc) · 1.66 KB
/
example1.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
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// https://play.golang.org/p/IwFKbnb1JO
// go build -race
// Sample program to show how to create race conditions in
// our programs. We don't want to do this.
package main
import (
"fmt"
"runtime"
"sync"
)
// counter is a variable incremented by all goroutines.
var counter int
// wg is used to wait for the program to finish.
var wg sync.WaitGroup
// main is the entry point for all Go programs.
func main() {
// Add a count of two, one for each goroutine.
wg.Add(2)
// Create two goroutines.
go incCounter()
go incCounter()
// Wait for the goroutines to finish.
wg.Wait()
fmt.Println("Final Counter:", counter)
}
// incCounter increments the package level counter variable.
func incCounter() {
for count := 0; count < 2; count++ {
// Capture the value of Counter.
value := counter
// Yield the thread and be placed back in queue.
// DO NOT USE IN PRODUCTION CODE!
runtime.Gosched()
// Increment our local value of Counter.
value++
// Store the value back into Counter.
counter = value
}
// Tell main we are done.
wg.Done()
}
/*
==================
WARNING: DATA RACE
Write by goroutine 6:
main.incCounter()
/Users/bill/.../example1/example1.go:52 +0x6f
Previous read by goroutine 7:
main.incCounter()
/Users/bill/.../example1/example1.go:42 +0x41
Goroutine 6 (running) created at:
main.main()
/Users/bill/.../example1/example1.go:30 +0x69
Goroutine 7 (running) created at:
main.main()
/Users/bill/.../example1/example1.go:31 +0x8a
==================
Final Counter: 2
Found 1 data race(s)
*/