dongshang5862 2018-03-21 02:58 采纳率: 0%
浏览 41
已采纳

RWMutex仍会提高比赛条件吗?

I have a seemingly innocent package that just makes a slice and protects it with a RWMutex. However, when I run it, it still complains about a race condition. What am I doing wrong? (playground)

type Ids struct {
    e []int64
    sync.RWMutex
}

func (i *Ids) Read() []int64 {
    i.RLock()
    defer i.RUnlock()

    return i.e
}


func (i *Ids) Append(int int64) {
    i.Lock()
    defer i.Unlock()

    i.e = append(i.e, int)
}

func main() {
    t := &Ids{e: make([]int64, 1)}

    for i := 0; i < 100; i++ {
        go func() {
            fmt.Printf("%v
", t.Read())
        }()

        go func() {
            t.Append(int64(i))
        }()
    }

    time.Sleep(time.Second * 10)
}

when run with -race, it returns (among other things):

==================
WARNING: DATA RACE
Read at 0x00c4200a0010 by goroutine 7:
  main.main.func2()
      .../main.go:38 +0x38

Previous write at 0x00c4200a0010 by main goroutine:
  main.main()
      .../main.go:32 +0x197

Goroutine 7 (running) created at:
  main.main()
      .../main.go:37 +0x173
==================
  • 写回答

1条回答 默认 最新

  • dongyuan9149 2018-03-21 03:06
    关注

    You are capturing the same variable i in multiple goroutines.

    One way to fix this is to modify your main for loop like so:

    for i := 0; i < 100; i++ {
        i := i  # look here
        go func() {
            fmt.Printf("%v
    ", t.Read())
        }()
    
        go func() {
            t.Append(int64(i))
        }()
    }
    

    This will ensure that you capture a different variable in the closure of the second goroutine each iteration of the for loop. In your example, the i passed to t.Append was the same i that was incremented by the for loop concurrently.

    I also recommend running go vet to catch such errors in the future. For more information, there is an entry on this problem in the Go FAQ that goes into more detail: https://golang.org/doc/faq#closures_and_goroutines

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要