尝试在Golang 1.8.3中重现数据竞争。

huangapple go评论82阅读模式
英文:

Trying to reproduce data race in Golang 1.8.3

问题

我尝试在我的应用程序中复现竞态条件,但是go build -race没有显示任何错误,即使在带有Linux的虚拟机上也是如此。

机器:
Windows:4个核心
Linux:2个核心(通过Vagrant和VirtualBox使用Ubuntu Xenial x64)

go版本:
Linux:go version go1.8.3 linux/amd64
Windows:go version go1.8.3 windows/amd64

go环境:
Linux:https://pastebin.com/pXURKfj3
Windows:https://pastebin.com/MTdjNnVW

示例1:https://play.golang.org/p/x-eD6bBrzz
示例2:https://play.golang.org/p/FSg8P7UP8p

我的示例中是否存在数据竞争?根据使用go build -race进行构建,两个示例都没有数据竞争。

如果上述示例没有数据竞争条件,是否有人可以给我发送一个真实的Golang数据竞争示例,以便我可以测试-race标志?我在网上也找到了一些示例,其中一些无法编译,一些示例显示没有数据竞争。

谢谢!

英文:

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!

答案1

得分: 2

你的两个示例都存在数据竞争问题。你可以通过以下方式找出数据竞争:

  1. go run -race program.go
  2. go test -race <package-name> 或者 go test -race -run=<testcase-func-name>
  3. go build -race program.go 然后执行程序
  4. go install -race <package-name> 然后执行程序

示例1: 数据竞争信息

$ 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)

示例2: 数据竞争信息

$ 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
英文:

Both of your examples has data race issue. You can find out data race in following ways.

  1. go run -race program.go
  2. go test -race &lt;package-name&gt; or go test -race -run=&lt;testcase-func-name&gt;
  3. go build -race program.go then execute the program
  4. go install -race &lt;package-name&gt; then execute the program

Example 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

huangapple
  • 本文由 发表于 2017年6月25日 07:58:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/44741975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定