普通网友 2017-02-24 09:07
浏览 36
已采纳

没有并发时的并发映射读写

The following go play example shows in a simplistic way what I have defined. I am passing a map as a copied value to a function (not a reference) as well as there is a recursion in my function which I assume passes by value as well.

https://play.golang.org/p/na6y6Wih4M

// this function has no write operations to dataMap, just reads
// dataMap, in fact, has no write operations since it was copied
func findParentAncestors(ID int, dataMap map[int]Data) []Data {
    results := []Data{}
    if _, ok := dataMap[ID]; ok {
        if parentData, ok := dataMap[dataMap[ID].ParentID]; ok {
            results = append(results, parentData)
            // recursion
            results = append(results, findParentAncestors(parentData.ID, dataMap)...)
        }
    }
    return results
}

PROBLEM: somehow along my program execution, which involves much more data than this example (obviusly), an error "fatal error: concurrent map read and map write" points function findParentAncestors():

main.findParentAncestors(0x39e3, 0xc82013ac90, 0x0, 0x0, 0x0)
    /opt/test/src/test.go:17 +0xa6 fp=0xc820269fb8 sp=0xc820269bd0
main.findParentAncestors(0x5d25, 0xc82013ac90, 0x0, 0x0, 0x0)
    /opt/test/src/test.go:21 +0x239 fp=0xc82026a3a0 sp=0xc820269fb8
  • 写回答

1条回答 默认 最新

  • douji2283 2017-02-24 09:49
    关注

    From your example, https://play.golang.org/p/na6y6Wih4M:

    // the orignalMap is defined elsewhere in the program (here represented)
    originalMap := map[int]Data{}
    originalMap[0] = Data{ID: 0, ParentID: -1, Name: "zero"}
    originalMap[1] = Data{ID: 1, ParentID: 0, Name: "one"}
    originalMap[2] = Data{ID: 2, ParentID: 1, Name: "two"}
    // copies the original map from a global location (here represented)
    copiedMap := originalMap
    // identifies ancestors unsing the copied map
    parents := findParentAncestors(2, copiedMap)
    

    This is a misnomer, copiedMap := originalMap, you are not copying the map.

    In Go all arguments are passed by value. It's equivalent to assigning each argument to each parameter. For a map, assignment, copiedMap := originalMap, or passing by value, findParentAncestors(2, copiedMap), copies the map descriptor which is a pointer to the map descriptor struct which contains a pointer to the map key-value data. Obviously you have a potential race condition if there are any writes to the map.

    You are using go version go1.6.3 linux/amd64, so run the race detector.

    Go 1.6 Release Notes

    Runtime

    The runtime has added lightweight, best-effort detection of concurrent misuse of maps. As always, if one goroutine is writing to a map, no other goroutine should be reading or writing the map concurrently. If the runtime detects this condition, it prints a diagnosis and crashes the program. The best way to find out more about the problem is to run the program under the race detector, which will more reliably identify the race and give more detail.

    Command go

    Compile packages and dependencies

    -race
      enable data race detection.
      Supported only on linux/amd64, freebsd/amd64, darwin/amd64 and windows/amd64.
    

    Also, compile and run your program using Go 1.8, the current release of Go, which significantly improves concurrent map misuse.

    Go 1.8 Release Notes

    Concurrent Map Misuse

    In Go 1.6, the runtime added lightweight, best-effort detection of concurrent misuse of maps. This release improves that detector with support for detecting programs that concurrently write to and iterate over a map.

    As always, if one goroutine is writing to a map, no other goroutine should be reading (which includes iterating) or writing the map concurrently. If the runtime detects this condition, it prints a diagnosis and crashes the program. The best way to find out more about the problem is to run the program under the race detector, which will more reliably identify the race and give more detail.

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

报告相同问题?

悬赏问题

  • ¥15 优质github账号直接兑换rmb,感兴趣伙伴可以私信
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)