当前位置: > > > > Go 中的竞争条件发生变化
Go 中的竞争条件发生变化
来源:stackoverflow
2024-04-19 13:48:34
0浏览
收藏
大家好,今天本人给大家带来文章《Go 中的竞争条件发生变化》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!
问题内容
《go in action》关于竞争条件的示例:
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
//1 fmt.Println("value=",value)
runtime.Gosched()
value++
counter = value
//2 fmt.Println("counter=",counter)
}
}
据说final counter最后应该是2,这里解释一下: “每个 goroutine 都会覆盖另一个 goroutine 的工作。当 goroutine 交换正在进行中。每个 goroutine 都会创建自己的计数器变量副本,并且 then 被替换为另一个 goroutine。当给 goroutine 时间再次执行时,计数器变量的值已经改变,但 goroutine 没有改变 更新其副本。相反,它继续增加它所拥有的副本并设置值 返回到计数器变量,替换其他 goroutine 执行的工作。”
我猜是环境原因,我的机器在go 1.10.3+win10下输出4。我想知道这本书出版后,go 发生了什么变化?如果我取消注释标记 1 最终计数器打印 2 或随机如果我取消注释标记 2。为什么?
解决方案
这本书是错误的。数据竞争的要点是结果是不确定的。
例如,final counter 可以是任何值。
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
//1 fmt.println("value=",value)
runtime.gosched()
value++
counter = value
//2 fmt.println("counter=",counter)
}
}
输出:
$ go version
go version devel +65fa2b615b Fri Aug 3 23:35:53 2018 +0000 linux/amd64
$ go run racer.go
Final Counter: 4
$ go run racer.go
Final Counter: 2
$ go run racer.go
Final Counter: 2
$ go run racer.go
Final Counter: 2
$ go run racer.go
Final Counter: 2
$ go run racer.go
Final Counter: 4
$ go run racer.go
Final Counter: 2
$ go run racer.go
Final Counter: 4
$ go run -race racer.go
==================
WARNING: DATA RACE
Read at 0x0000005e4600 by goroutine 7:
main.incCounter()
/home/peter/gopath/src/racer.go:27 +0x6f
Previous write at 0x0000005e4600 by goroutine 6:
main.incCounter()
/home/peter/gopath/src/racer.go:33 +0x90
Goroutine 7 (running) created at:
main.main()
/home/peter/gopath/src/racer.go:17 +0x89
Goroutine 6 (finished) created at:
main.main()
/home/peter/gopath/src/racer.go:16 +0x68
==================
Final Counter: 4
Found 1 data race(s)
exit status 66
$
今天关于《Go 中的竞争条件发生变化》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注米云公众号!
