Reputation: 159
I try to reproduce race condition in my app, but go build -race
doesnt show any error, even on virtual machine with Linux onboard.
Machine:
Windows: 4 cores
Linux: 2 cores (Ubuntu Xenial x64 via Vagrant and VirtualBox)
go version:
Linux: go version go1.8.3 linux/amd64
Windows: go version go1.8.3 windows/amd64
go env:
Linux: https://pastebin.com/pXURKfj3
Windows: https://pastebin.com/MTdjNnVW
Example 1: https://play.golang.org/p/x-eD6bBrzz
Example 2: https://play.golang.org/p/FSg8P7UP8p
Is my examples has data race? Both of them no according to build with go build -race
If examples above hasnt data race condition, could somebody please send me example where i can see a real data race in Golang, so i can test the -race flag? I also found some examples on the web, some of them doesnt compiling, some of them same shows that there is no data race.
Thank you!
Upvotes: 0
Views: 137
Reputation: 4781
Both of your examples has data race issue. You can find out data race in following ways.
go run -race program.go
go test -race <package-name>
or go test -race -run=<testcase-func-name>
go build -race program.go
then execute the programgo install -race <package-name>
then execute the programExample 1: data race info
$ go build -race datarace-try1.go
$ ./datarace-try1
8
==================
WARNING: DATA RACE
Read at 0x00c4200761a8 by goroutine 7:
main.main.func1()
/Users/jeeva/go_playground/datarace-try1.go:23 +0x74
Previous write at 0x00c4200761a8 by goroutine 6:
main.main.func1()
/Users/jeeva/go_playground/datarace-try1.go:23 +0x8d
Goroutine 7 (running) created at:
main.main()
/Users/jeeva/go_playground/datarace-try1.go:25 +0xee
Goroutine 6 (running) created at:
main.main()
/Users/jeeva/go_playground/datarace-try1.go:25 +0xee
==================
187410
Found 1 data race(s)
Example 2: data race info
$ go build -race datarace-try2.go
$ ./datarace-try2
==================
WARNING: DATA RACE
Read at 0x00c420078178 by main goroutine:
main.main()
/Users/jeeva/go_playground/datarace-try2.go:10 +0x12e
Previous write at 0x00c420078178 by goroutine 6:
main.main.func1()
/Users/jeeva/go_playground/datarace-try2.go:15 +0xd4
Goroutine 6 (finished) created at:
main.main()
/Users/jeeva/go_playground/datarace-try2.go:17 +0xf1
==================
==================
WARNING: DATA RACE
Read at 0x00c420078178 by goroutine 7:
main.main.func2()
/Users/jeeva/go_playground/datarace-try2.go:20 +0x3f
Previous write at 0x00c420078178 by goroutine 6:
main.main.func1()
/Users/jeeva/go_playground/datarace-try2.go:15 +0xd4
Goroutine 7 (running) created at:
main.main()
/Users/jeeva/go_playground/datarace-try2.go:23 +0x11d
Goroutine 6 (finished) created at:
main.main()
/Users/jeeva/go_playground/datarace-try2.go:17 +0xf1
==================
finish
Found 2 data race(s)
exit status 66
Upvotes: 2