比赛条件。不明白为什么

时间:2024-03-30

浏览:234

作者:佚名

大家好,今天本人给大家带来文章《比赛条件。不明白为什么》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!

问题内容

当我运行代码时会发生竞争条件。它是并发安全存储的简单实现。当我将 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()
    }
}


正确答案


在 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?

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

考虑使用 sync.WaitGroup

以上就是《比赛条件。不明白为什么》的详细内容,更多关于的资料请关注golang学习网公众号!

本文内容来源于互联网,如有侵权请联系删除。

相关文章

更多

热门专题

更多
首页