最近在学习MIT的分布式课程6.824的过程当中,使用Go实现Raft协议时遇到了一些问题。参见以下代码:git
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呢。github
虽然不明白发生了什么,但知道应该是循环中引入的 goroutine 致使的。通过Google,发现Go的wiki中就有一个页面 Common Mistake - Using goroutines on loop iterator variables 专门提到了这个问题,看来真的是很 common 啊,笑哭~golang
初学者常常会使用以下代码来并行处理数据:编程
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.oop
以上代码正确的写法为:学习
for val := range values { go func(val interface{}) { fmt.Println(val) }(val) }
在这里将 val 做为一个参数传入 goroutine 中,每一个 val 都会被独立计算并保存到 goroutine 的栈中,从而获得预期的结果。this
另外一种方法是在循环内定义新的变量,因为在循环内定义的变量在循环遍历的过程当中是不共享的,所以也能够达到一样的效果:调试
for i := range valslice { val := valslice[i] go func() { fmt.Println(val) }() }
对于文章开头提到的那个问题,最简单的解决方案就是在循环内加一个临时变量,并将后面 goroutine 内的 i 都替换为这个临时变量便可:
server := i
,