doufu8127 2017-06-24 23:58 采纳率: 100%
浏览 26
已采纳

尝试在Golang 1.8.3中重现数据竞赛

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条回答 默认 最新

  • duanshang3230 2017-06-25 00:59
    关注

    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 <package-name> or go test -race -run=<testcase-func-name>
    3. go build -race program.go then execute the program
    4. go install -race <package-name> 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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题