当前位置:  开发笔记 > 编程语言 > 正文

为什么这个循环被认为是数据竞争[Golang]

如何解决《为什么这个循环被认为是数据竞争[Golang]》经验,为你挑选了1个好方法。

我有一个循环,我得到这个错误,我正在运行 go run -race main.go

这是有问题的循环:

var wg sync.WaitGroup

for _, member := range p.Members { << Line 254
    go func() {
        wg.Add(1)
        _, err := db.Exec("UPDATE party_members SET active = ? WHERE steamid = ?", false, member.SteamID) << Line 257
        if err != nil {
            log.Println(err)
        }
        _, err = db.Exec("INSERT INTO party_members SET belongs_to =?, active = ?, steamid = ?", partyUUID, true, member.SteamID)
        if err != nil {
            log.Println(err)
        }
        wg.Done()
    }() << Line 266
}

然后这是我得到的错误:

WARNING: DATA RACE
Read by goroutine 44:
  BLAH/controllers/partybot.func·001()
      /Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:257 +0x136

Previous write by goroutine 43:
  BLAH/controllers/partybot.(*PartialParty).SendReadyCheck()
      /Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:254 +0xda5

Goroutine 44 (running) created at:
  BLAH/controllers/partybot.(*PartialParty).SendReadyCheck()
      /Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:266 +0xf13

Goroutine 43 (finished) created at:
  BLAH/controllers/partybot.(*PartyHub).SortMemberIntoParty()
      /Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:173 +0xdb9
  BLAH/controllers/partybot.(*Connection).readPump()
      /Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:359 +0x1872
  BLAH/controllers/partybot.WSPartyBot()
      /Users/Dev/gocode/src/BLAH/controllers/partybot/partybot.go:440 +0x4f0
  net/http.HandlerFunc.ServeHTTP()
      /usr/local/go/src/net/http/server.go:1265 +0x4e
  github.com/gorilla/mux.(*Router).ServeHTTP()
      /Users/Dev/gocode/src/github.com/gorilla/mux/mux.go:98 +0x377
  BLAH/controllers/common.func·001()
      /Users/Dev/gocode/src/BLAH/controllers/common/common.go:36 +0xc2
  net/http.HandlerFunc.ServeHTTP()
      /usr/local/go/src/net/http/server.go:1265 +0x4e
  net/http.(*ServeMux).ServeHTTP()
      /usr/local/go/src/net/http/server.go:1541 +0x20c
  net/http.serverHandler.ServeHTTP()
      /usr/local/go/src/net/http/server.go:1703 +0x1f6
  net/http.(*conn).serve()
      /usr/local/go/src/net/http/server.go:1204 +0x1087
==================

我不知道为什么这是一场数据竞赛,因为如果有人能告诉我为什么这会很好,我对Go来说还是新手,我标记了有问题的线条.



1> superfell..:

问题是goroutine正在通过闭包访问变量'member',并且在go中它被到达时被评估(即它在goroutine执行时看到成员的值,而不是在通过go创建时的值) func()... call.成员变量正在被循环更新,所以循环更新和你已经开始读取它们的goroutines之间存在竞争.(你会发现如果你进一步挖掘你的数据库没有'从集合中看到确切的成员集.)通常,您可以通过在循环中强制评估变量来解决此问题,方法是创建局部变量,或者将其作为参数添加到func,例如

var wg sync.WaitGroup

for _, member := range p.Members {
    wg.Add(1)
    m := member
    go func() {
        _, err := db.Exec("UPDATE party_members SET active = ? WHERE steamid = ?", false, m.SteamID)
        ...
        wg.Done()
    }() 
}

要么

var wg sync.WaitGroup

for _, member := range p.Members {
    wg.Add(1)
    go func(m memberType) {
        _, err := db.Exec("UPDATE party_members SET active = ? WHERE steamid = ?", false, m.SteamID)
        ...
        wg.Done()
    }(member) 
}

另请注意,您需要从循环中调用wg.Add(1),而不是从goroutine本身调用.

推荐阅读
ERIK又
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有