【问题标题】:concurrent map iteration and map write error in Golang 1.8Golang 1.8 中的并发映射迭代和映射写入错误
【发布时间】:2017-07-16 03:49:22
【问题描述】:

所以我有这个功能..

func Set(firstSet map[string][]App, store map[string]*Parsed) map[string][string]struct{} {
    s := make(map[string]map[string]struct{})
    for dmn, parsed := range store {
        for cId, apps := range firstSet {
            if _, ok := s[dmn]; !ok {
                s[dmn] = make(map[string]struct{})
            }
            s[dmn][cId] = struct{}{}
        }
    }

    return s
}

该函数的第 3 行(对于 dmn,已解析:= 范围存储)在 Golang 1.8 中给了我错误并发映射迭代和映射写入错误。有什么想法吗?

【问题讨论】:

    标签: go


    【解决方案1】:

    看起来像Concurrent Map Misuse 。可能您的函数是从不同的 gorotine 调用的。尝试将函数体包含在 mutex.Lock()/Unlock() 中,以便您的地图可以安全地并发使用。

    【讨论】:

      【解决方案2】:

      Golang 1.8 中增加了一个增强的并发访问检查,这个是runtime/hashmap.go:736中的源码,

      if h.flags&hashWriting != 0 {
          throw("concurrent map iteration and map write")
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-10-31
        • 1970-01-01
        • 1970-01-01
        • 2011-05-29
        • 1970-01-01
        • 2016-07-10
        • 1970-01-01
        相关资源
        最近更新 更多