为什么不 read/write 其内容的结构的方法仍然导致竞争情况?

Why does the method of a struct that does not read/write its contents still cause a race case?

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

这是shorthand

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
}