【发布时间】:2017-10-01 23:24:06
【问题描述】:
完整代码在这里:https://play.golang.org/p/ggUoxtcv5m
go run -race main.go 说那里存在竞争条件,我无法解释。
不过,程序会输出正确的最终结果。
本质:
type SafeCounter struct {
c int
sync.Mutex
}
func (c *SafeCounter) Add() {
c.Lock()
c.c++
c.Unlock()
}
var counter *SafeCounter = &SafeCounter{} // global
在增量器中使用*SafeCounter:
func incrementor(s string) {
for i := 0; i < 20; i++ {
x := counter
x.Add()
counter = x
}
}
incrementor 方法在 main 中产生了两次:
func main() {
go incrementor()
go incrementor()
// some other non-really-related stuff like
// using waitGroup is ommited here for problem showcase
}
所以,正如我所说,go run -race main.go 总是会说找到了比赛条件。
另外,最终结果总是正确的(至少我已经多次运行这个程序,它总是说最终计数器是 40,这是正确的)。 但是,程序在开始时会打印不正确的值,因此您可以得到类似的结果:
Incrementor1: 0 Counter: 2
Incrementor2: 0 Counter: 3
Incrementor2: 1 Counter: 4
// ang the rest is ok
所以,那里缺少打印出1。
有人可以解释为什么我的代码存在竞争条件吗?
【问题讨论】:
标签: go mutex race-condition