网站地图    收藏   

主页 > 后端 > Golang >

关于Golang中for-loop与goroutine的问题详解

来源:自学PHP网    时间:2019-08-07 16:47 作者:小飞侠 阅读:

[导读] 关于Golang中for-loop与goroutine的问题详解...

背景

最近在学习MIT的分布式课程6.824的过程中,使用Go实现Raft协议时遇到了一些问题。分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

参见如下代码:

for i := 0; i < len(rf.peers); i++ {
  DPrintf("i = %d", i)

  if i == rf.me {
   DPrintf("skipping myself #%d", rf.me)
   continue
  }

  go func() {
   DPrintf("len of rf.peers = %d", len(rf.peers))
   DPrintf("server #%d sending request vote to server %d", rf.me, i)
   reply := &RequestVoteReply{}
   ok := rf.sendRequestVote(i, args, reply)
   if ok && reply.VoteGranted && reply.Term == rf.currentTerm {
    rf.voteCount++
    if rf.voteCount > len(rf.peers)/2 {
     rf.winElectionCh <- true
    }
   }
  }()
}

其中,peers切片的长度为3,因此最高下标为2,在非并行编程中代码中的for-loop应该是很直观的,我当时并没有意识到有什么问题。可是在调试过程中,一直在报 index out of bounds 错误。调试信息显示i的值为3,当时就一直想不明白循环条件明明是 i < 2,怎么会变成3呢。

分析

虽然不明白发生了什么,但知道应该是循环中引入的 goroutine 导致的。经过Google,发现Go的wiki中就有一个页面 Common Mistake - Using goroutines on loop iterator variables 专门提到了这个问题,看来真的是很 common 啊,笑哭~

初学者经常会使用如下代码来并行处理数据:

for val := range values {
 go val.MyMethod()
}

或者使用闭包(closure):

for val := range values {
 go func() {
  fmt.Println(val)
 }()
}

这里的问题在于 val 实际上是一个遍历了切片中所有数据的单一变量。由于闭包只是绑定到这个 val 变量上,因此极有可能上面的代码的运行结果是所有 goroutine 都输出了切片的最后一个元素。这是因为很有可能当 for-loop 执行完之后 goroutine 才开始执行,这个时候 val 的值指向切片中最后一个元素。

The val variable in the above loops is actually a single variable that takes on the value of each slice element. Because the closures are all only bound to that one variable, there is a very good chance that when you run this code you will see the last element printed for every iteration instead of each value in sequence, because the goroutines will probably not begin executing until after the loop.

解决方法

以上代码正确的写法为:

for val := range values {
 go func(val interface{}) {
  fmt.Println(val)
 }(val)
}

在这里将 val 作为一个参数传入 goroutine 中,每个 val 都会被独立计算并保存到 goroutine 的栈中,从而得到预期的结果。

另一种方法是在循环内定义新的变量,由于在循环内定义的变量在循环遍历的过程中是不共享的,因此也可以达到同样的效果:

for i := range valslice {
 val := valslice[i]
 go func() {
  fmt.Println(val)
 }()
}

对于文章开头提到的那个问题,最简单的解决方案就是在循环内加一个临时变量,并将后面 goroutine 内的 i 都替换为这个临时变量即可:

server := i

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对自学php网的支持。

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论