【问题标题】:Race condition. Cannot figure out why比赛条件。无法弄清楚为什么
【发布时间】:2021-04-22 15:13:52
【问题描述】:

当我运行我的代码时会出现竞争条件。它是并发安全存储的简单实现。当我将 get() 方法中的接收者更改为 (p *storageType) 时,竞争条件消失了。我很困惑。我需要有人可以向我解释这种行为。

package main

type storageType struct {
    fc    chan func()
    value int
}

func newStorage() *storageType {
    p := storageType{
        fc: make(chan func()),
    }
    go p.run()
    return &p
}

func (p storageType) run() {
    for {
        (<-p.fc)()
    }
}

func (p *storageType) set(s int) {
    p.fc <- func() {
        p.value = s
    }
}

func (p storageType) get() int {
    res := make(chan int)
    p.fc <- func() {
        res <- p.value
    }
    return <-res
}

func main() {

    storage := newStorage()

    for i := 0; i < 1000; i++ {
        go storage.set(i)
        go storage.get()
    }
}

【问题讨论】:

  • 什么机器/os/go版本?我试过8核/macos/go1.10;和16核/macos/go1.14;它似乎对那些工作正常。
  • linux, go1.16.3, 64bit

标签: go pointers struct race-condition


【解决方案1】:

main() 中,storage 变量的类型为*storageType。如果storageType.Get() 有值接收者,那么storage.get() 表示(*storage).get()

get() 调用将storageType 作为接收方,因此必须取消引用storage 指针变量才能进行复制(将用作接收方值)。这种复制意味着必须读取指向的storageType 结构的值。但是这种读取与读取和写入结构(其value 字段)的run() 方法不同步。

如果您将get() 的接收者更改为指针(*storageType 类型),那么接收者将再次成为副本,但这一次它将是指针的副本,而不是指向的结构。所以不会发生结构体的非同步读取。

查看可能的重复:Why does the method of a struct that does not read/write its contents still cause a race case?

【讨论】:

  • 我现在明白了!非常感谢!
【解决方案2】:

第一个:您的 main 函数不会等待所有 goroutine 完成。当main 执行时,所有的 goroutine 都会被强制返回。

考虑使用sync.WaitGroup

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多