[英]Golang: goroutine infinite-loop
When an fmt.Print()
line is removed from the code below, code runs infinitely. 从下面的代码中删除
fmt.Print()
行时,代码无限运行。 Why? 为什么?
package main
import "fmt"
import "time"
import "sync/atomic"
func main() {
var ops uint64 = 0
for i := 0; i < 50; i++ {
go func() {
for {
atomic.AddUint64(&ops, 1)
fmt.Print()
}
}()
}
time.Sleep(time.Second)
opsFinal := atomic.LoadUint64(&ops)
fmt.Println("ops:", opsFinal)
}
The Go By Example article includes : Go by Example文章包括 :
// Allow other goroutines to proceed.
runtime.Gosched()
The fmt.Print()
plays a similar role, and allows the main()
to have a chance to proceed. fmt.Print()
扮演类似的角色,并允许main()
有机会继续。
A export GOMAXPROCS=2
might help the program to finish even in the case of an infinite loop, as explained in " golang: goroute with select doesn't stop unless I added a fmt.Print()
". export GOMAXPROCS=2
可能有助于程序在无限循环的情况下完成,如“ golang:goroute with select不会停止,除非我添加了fmt.Print()
”。
fmt.Print()
explicitly passes control to some syscall stufffmt.Print()
显式地将控制传递给某些系统调用
Yes, go1.2+ has pre-emption in the scheduler 是的, go1.2 +在调度程序中有优先权
In prior releases, a goroutine that was looping forever could starve out other goroutines on the same thread, a serious problem when
GOMAXPROCS
provided only one user thread.在以前的版本中,永远循环的goroutine可能会在同一个线程上饿死其他goroutine,这是
GOMAXPROCS
只提供一个用户线程时的一个严重问题。In Go 1.2, this is partially addressed: The scheduler is invoked occasionally upon entry to a function.
在Go 1.2中,部分解决了这个问题:在进入函数时偶尔会调用调度程序。 This means that any loop that includes a (non-inlined) function call can be pre-empted, allowing other goroutines to run on the same thread.
这意味着任何包含(非内联)函数调用的循环都可以被抢占,允许其他goroutine在同一个线程上运行。
Notice the emphasis (that I put): it is possible that in your example the for loop atomic.AddUint64(&ops, 1)
is inlined. 注意重点(我提出):在你的例子中,有可能是for循环
atomic.AddUint64(&ops, 1)
被内联。 No pre-emption there. 那里没有先发制人。
Update 2017: Go 1.10 will get rid of GOMAXPROCS
. 2017年更新: Go 1.10将摆脱
GOMAXPROCS
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.