duanla4959 2016-06-10 18:45
浏览 55
已采纳

Golang数据竞赛,拥有66个退出状态

I have the following code, and I am having data race. The Round function periodically check runs the function to delete the contents of the map As I read here: Is it safe to remove selected keys from Golang map within a range loop?

Removing the data from map is safe, but I am having data race

package main

import (
    "fmt"
    "sync"
    "time"
)

type City struct {
    ID string
}

type Map struct {
    sync.RWMutex
    Data map[string]City
}

var done = make(chan struct{})

func (m *Map) Round() {
    for {
        select {
        case <-time.After(2 * time.Second):
            for i, v := range m.Data {
                fmt.Println("-----", v)
                delete(m.Data, i)
            }
        case <-done:
            println("bye")
            break
        }
    }
}

func (m *Map) Add(id string, h City) {
    m.Lock()
    m.Data[id] = h
    m.Unlock()
}

func main() {
    m := Map{}
    m.Data = make(map[string]City)

    m.Data["Ottowa"] = City{"Canada"}
    m.Data["London"] = City{"GB"}
    m.Data["malafya"] = City{"malafya"}

    go m.Round()

    for i := 0; i < 4; i++ {
        go func() {
            time.Sleep(2 * time.Second)
            go m.Add("uz", City{"CityMakon"})
            go m.Add("uzb", City{"CityMakon"})
        }()
    }
    time.Sleep(5 * time.Second)
    done <- struct{}{}
}

the output:

----- {Canada}
----- {GB}
----- {malafya}
==================
WARNING: DATA RACE
Write by goroutine 12:
  runtime.mapassign1()
      /usr/lib/golang/src/runtime/hashmap.go:411 +0x0
  main.(*Map).Add()
      /home/narkoz/elixir/round.go:37 +0xaa

Previous write by goroutine 6:
  runtime.mapdelete()
      /usr/lib/golang/src/runtime/hashmap.go:511 +0x0
  main.(*Map).Round()
      /home/narkoz/elixir/round.go:26 +0x3a9

Goroutine 12 (running) created at:
  main.main.func1()
      /home/narkoz/elixir/round.go:54 +0x8c

Goroutine 6 (running) created at:
  main.main()
      /home/narkoz/elixir/round.go:49 +0x2af
==================
----- {CityMakon}
----- {CityMakon}
Found 1 data race(s)
exit status 66

But when I change the value type of the map to int or string, there is not data race.

What solution do you recommend ?

  • 写回答

1条回答 默认 最新

  • douxuzui4590 2016-06-10 18:51
    关注

    UPDATES:

    But when I change the value type of the map to int or string, there is not data race.

    I tested your code further. Changing the map's value type to int or string continues to yield the race condition. Try run it in a while loop on your shell, you will see what I mean:

    $ while true; do go run -race main.go; done
    

    There shouldn't be any differences between the value types.


    As reported by the race detectors, there are two different race conditions. The first race (which you've fixed) happens between the read (of i) at line 54 and the write (to i) at line 51. This happens because your goroutine closure holds a reference to i, which is changed by the for loop in your main goroutine. You can resolve it by either getting rid of the println(">>", i) or passing i into your closure like this:

    for i := 0; i < 4; i++ {
      go func(index int) {
        time.Sleep(2 * time.Second)
        println(">>", index)
        go m.Add("uz", City{"CityMakon"})
        go m.Add("uzb", City{"CityMakon"})
      }(i)
    }
    

    The second race condition happens between the assignment on line 37 (m.Data[id] = h) and the delete in line 25 (delete(m.Data, i)). The race detector marks this as a race condition because it can't guarantee the Happen Before constraint on your code. You can resolve this by either:

    Locking the delete statement:

    m.Lock()
    delete(m.Data, i)
    m.Unlock()
    

    Or alternately, extract the two cases in your Round() method into two methods, ranging over the channel:

    func (m *Map) Round() {
      for i, v := range m.Data {
        fmt.Println("-----", v)
        delete(m.Data, i)
      }
    }
    
    func (m *Map) Done() {
      for range done {
        println("bye")
        break
      }
    }
    
    func main() {
      // ...
      go Round()
      go Done()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题