相关疑难解决方法(0)

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

戴夫·切尼博客,下面的代码显然会导致比赛的情况下仅通过改变来解决func (RPC) version() intfunc (*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 := …
Run Code Online (Sandbox Code Playgroud)

concurrency struct go race-condition goroutine

5
推荐指数
1
解决办法
77
查看次数

标签 统计

concurrency ×1

go ×1

goroutine ×1

race-condition ×1

struct ×1