dpdhf02040 2018-08-18 16:24
浏览 80
已采纳

如果并发进程向全局变量写入相同的值会怎样?

I'm just wondering if there is potential for corruption as a result of writing the same value to a global variable at the same time. My brain is telling me there is nothing wrong with this because its just a location in memory, but I figure I should probably double check this assumption.

I have concurrent processes writing to a global map var linksToVisit map[string]bool. The map is actually tracking what links on a website need to be further crawled.

However it can be the case that concurrent processes may have the same link on their respective pages and therefore each will mark that same link as true concurrently. There's nothing wrong with NOT using locks in this case right? NOTE: I never change the value back to false so either the key exists and it's value is true or it doesn't exist.

I.e.

var linksToVisit = map[string]bool{}

... 
// somewhere later a goroutine finds a link and marks it as true
// it is never marked as false anywhere
linksToVisit[someLink] = true 
  • 写回答

4条回答 默认 最新

  • dqpu4988 2018-08-18 19:20
    关注

    What happens if concurrent processes write to a global variable the same value?

    The results of a data race are undefined.

    Run the Go data race detector.

    References:

    Wikipedia: Race condition

    Benign Data Races: What Could Possibly Go Wrong?

    The Go Blog: Introducing the Go Race Detector

    Go: Data Race Detector


    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.


    For example,

    package main
    
    import "time"
    
    var linksToVisit = map[string]bool{}
    
    func main() {
        someLink := "someLink"
        go func() {
            for {
                linksToVisit[someLink] = true
            }
        }()
        go func() {
            for {
                linksToVisit[someLink] = true
            }
        }()
        time.Sleep(100 * time.Millisecond)
    }
    

    Output:

    $ go run racer.go
    fatal error: concurrent map writes
    $
    
    $ go run -race racer.go
    
    ==================
    WARNING: DATA RACE
    Write at 0x00c000078060 by goroutine 6:
      runtime.mapassign_faststr()
          /home/peter/go/src/runtime/map_faststr.go:190 +0x0
      main.main.func2()
          /home/peter/gopath/src/racer.go:16 +0x6a
    
    Previous write at 0x00c000078060 by goroutine 5:
      runtime.mapassign_faststr()
          /home/peter/go/src/runtime/map_faststr.go:190 +0x0
      main.main.func1()
          /home/peter/gopath/src/racer.go:11 +0x6a
    
    Goroutine 6 (running) created at:
      main.main()
          /home/peter/gopath/src/racer.go:14 +0x88
    
    Goroutine 5 (running) created at:
      main.main()
          /home/peter/gopath/src/racer.go:9 +0x5b
    ==================
    
    fatal error: concurrent map writes
    
    $
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效