duanlangwen9597 2019-01-18 07:32
浏览 61

如何解决“使用go-build-race工具检查种族状况的一个问题”?

At first, I know the code has some race condition , so I used "go build -race" command to check it and I want to see how the result shows, when I run, at the first time, it shows the one result as follows, and then run again shows the second one, it has two different results, and I don't know why, and is any one can tell me the reason, and how the code was executed ?, Thanks a lot very much.

Source Code:

package main

import (
    "fmt"
    "runtime"
    "sync"
)

var (
    counter int
    wg sync.WaitGroup
)

func main() {
    wg.Add(2)

    go incCounter(1)
    go incCounter(2)

    wg.Wait()

    fmt.Println("Final Counter:", counter)

}

func incCounter(id int) {
    defer wg.Done()

    for count := 0; count < 2; count++ {
        value := counter
        // switch goroutine
        runtime.Gosched()
        value++
        counter = value
    }
}

When I use go build -race tool to check race condition, it shows two different results ,as follows:

One Result:

==================
WARNING: DATA RACE
Write at 0x0000005fb2d0 by goroutine 7:
  main.incCounter()
      D:/Go/projects/hello-world/src/concurrency/list7/m.go:34 +0x97

Previous read at 0x0000005fb2d0 by goroutine 6:
  main.incCounter()
      D:/Go/projects/hello-world/src/concurrency/list7/m.go:30 +0x76

Goroutine 7 (running) created at:
  main.main()
      D:/Go/projects/hello-world/src/concurrency/list7/m.go:18 +0x90

Goroutine 6 (running) created at:
  main.main()
      D:/Go/projects/hello-world/src/concurrency/list7/m.go:17 +0x6f
==================
==================
WARNING: DATA RACE
Write at 0x0000005fb2d0 by goroutine 6:
  main.incCounter()
      D:/Go/projects/hello-world/src/concurrency/list7/m.go:34 +0x97

Previous write at 0x0000005fb2d0 by goroutine 7:
  main.incCounter()
      D:/Go/projects/hello-world/src/concurrency/list7/m.go:34 +0x97

Goroutine 6 (running) created at:
  main.main()
      D:/Go/projects/hello-world/src/concurrency/list7/m.go:17 +0x6f

Goroutine 7 (running) created at:
  main.main()
      D:/Go/projects/hello-world/src/concurrency/list7/m.go:18 +0x90
==================
Final Counter: 2
Found 2 data race(s)

Second Result:

==================
WARNING: DATA RACE
Read at 0x0000005fb2d0 by goroutine 7:
  main.incCounter()
      D:/Go/projects/hello-world/src/concurrency/list7/m.go:30 +0x76

Previous write at 0x0000005fb2d0 by goroutine 6:
  main.incCounter()
      D:/Go/projects/hello-world/src/concurrency/list7/m.go:34 +0x97

Goroutine 7 (running) created at:
  main.main()
      D:/Go/projects/hello-world/src/concurrency/list7/m.go:18 +0x90

Goroutine 6 (finished) created at:
  main.main()
      D:/Go/projects/hello-world/src/concurrency/list7/m.go:17 +0x6f
==================
Final Counter: 4
Found 1 data race(s)

They are the two different results.

  • 写回答

1条回答 默认 最新

  • dongshen9058 2019-01-18 08:09
    关注

    I suggest you to explore scheduling in go (good article from ardanlabs).

    The short answer is you don't control an execution order, go runtime does. Each execution of the same program won’t produce the same execution trace. Race detector tracks "racy" behaviour on each run and results directly depend on the decision of the scheduler.

    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题