go - 为什么不读/写其内容的结构方法仍然会导致竞争情况?

标签 go struct concurrency race-condition goroutine

来自 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/

相关文章:

go - 如何解决导入周期不允许的问题,尽管我正在使用界面?

C int 未存储

c - 为什么根在二叉树中总是为空

go - 导入本地模块

go - 如何用 gorm 填充和嵌入数组?

multithreading - 锁里面的AtomicInteger有什么用

java - 正确调用 Hashmap 的 get() 和 put() 方法。

java - 如何比较线程对象

go - 如何在 net.Con 中正确分块消息并阻止广播 sleep

C 从字符到函数名