duanhan8757 2015-09-17 13:59
浏览 30

用于println和fmt.Println的竞争检测器中的输出不同

I have a different output for println and fmt.Println in race detector which I couldn't explain. I expected both to be race, or at least both to be no race.

package main

var a int

func f() {
    a = 1
}

func main() {
    go f()
    println(a)
}

And, it finds race condition as expected.

0
==================
WARNING: DATA RACE
Write by goroutine 5:
  main.f()
/home/felmas/test.go:6 +0x30

Previous read by main goroutine:
  main.main()
/home/felmas/test.go:11 +0x4d

Goroutine 5 (running) created at:
  main.main()
/home/felmas/test.go:10 +0x38
==================
Found 1 data race(s)

However, this one runs without any detected race.

package main

import "fmt"

var a int

func f() {
    a = 1
}

func main() {
    go f()
    fmt.Println(a)
}

To my knowledge, no race is detected doesn't mean there is no race so is this one of these deficiencies or is there a deeper explanation since println is builtin and quite special?

  • 写回答

1条回答 默认 最新

  • doolo00026 2015-09-21 19:51
    关注

    The race detector is a dynamic testing tool and no static analysis. In order to get reliable results from the race detector, you should strife for a high test coverage of your program, preferable by writing lots of benchmarks using multiple processes (by setting GOMAXPROCS > 1, GOMAXPROCS=NumCPU is the default for Go 1.5) and use a continuous integration tool that executes those tests regularly.

    The race detector does not report any false positives so you should take every output serious. On the other hand it might not detect every race on every run, depending on the order goroutines and processes are scheduled.

    In your example, wrapping everything in a tight loop and re-executing the tests reports the race correctly in both cases.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集