来自 the Dave Cheney Blog ,下面的代码显然会导致一个竞争案例,只需将 func (RPC) version() int
更改为 func (*RPC) version() int
即可解决:
package main
import (
"fmt"
"time"
)
type RPC struct {
result int
done chan struct{}
}
func (rpc *RPC) compute() {
time.Sleep(time.Second) // strenuous computation intensifies
rpc.result = 42
close(rpc.done)
}
func (RPC) version() int {
return 1 // never going to need to change this
}
func main() {
rpc := &RPC{done: make(chan struct{})}
go rpc.compute() // kick off computation in the background
version := rpc.version() // grab some other information while we're waiting
<-rpc.done // wait for computation to finish
result := rpc.result
fmt.Printf("RPC computation complete, result: %d, version: %d\n", result, version)
}
查看代码几次后,我很难相信代码中存在竞争情况。但是,当使用 --race 运行时,它声称在 rpc.result=42
处有一个写入,在 version := rpc.version()
处有一个先前的读取。我理解写入,因为 goroutine 改变了 rpc.result
的值,但是读取呢?读取发生在 version()
方法的哪个位置?它不触及 rpc 的任何值,只返回 1。
我想了解以下内容:
1) 为什么该特定行被视为对 rpc 结构的读取?
2) 为什么将 RPC
更改为 *RPC
可以解决竞争问题?
最佳答案
当你有一个像这样的带有值接收者的方法时:
func (RPC) version() int {
return 1 // never going to need to change this
}
然后你调用这个方法:
version := rpc.version() // grab some other information while we're waiting
必须从值 rpc
制作一个副本,该副本将传递给方法(用作接收值)。
因此,当一个 goroutine go rpc.compute()
正在运行并正在修改 rpc
结构值时 (rpc.result = 42
) ,主 goroutine 正在复制整个 rpc
结构值。那里!这是一场比赛。
将接收者类型修改为指针时:
func (*RPC) version() int {
return 1 // never going to need to change this
}
然后你调用这个方法:
version := rpc.version() // grab some other information while we're waiting
这是
的简写version := (&rpc).version()
这将 rpc
值的地址传递给 RPC.version()
,它仅使用指针作为接收者,因此不会对 rpc
结构值。由于 RPC.version()
中没有使用/读取结构中的任何内容,因此不存在竞争。
注意:
请注意,如果 RPC.version()
将读取 RPC.result
字段,这也将是一场竞赛,因为一个 goroutine 会修改它,而主 goroutine 会阅读:
func (rpc *RPC) version() int {
return rpc.result // RACE!
}
注释 #2:
另请注意,如果 RPC.version()
将读取 RPC
的另一个字段,该字段未在 RPC.compute()
中修改,那不会是一场比赛,例如:
type RPC struct {
result int
done chan struct{}
dummy int
}
func (rpc *RPC) version() int {
return rpc.dummy // Not a race
}
关于go - 为什么不读/写其内容的结构方法仍然会导致竞争情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39042074/