为什么这段 Go 代码会阻塞?

huangapple go评论80阅读模式
英文:

Why is this Go code blocking?

问题

我写了以下程序:

package main

import (
	"fmt"
)

func processevents(list chan func()) {
	for {
		//a := <-list
		//a()
	}
}

func test() {
	fmt.Println("Ho!")
}

func main() {

	eventlist := make(chan func(), 100)

	go processevents(eventlist)

	for {
		eventlist <- test
		fmt.Println("Hey!")
	}
}

由于通道eventlist是一个带缓冲的通道,我认为我应该得到100次输出"Hey!",但实际上只显示了一次。我的错误在哪里?

英文:

I wrote the following program:

package main

import (
	&quot;fmt&quot;
)

func processevents(list chan func()) {
	for {
		//a := &lt;-list
		//a()
	}
}

func test() {
	fmt.Println(&quot;Ho!&quot;)
}

func main() {

	eventlist := make(chan func(), 100)

	go processevents(eventlist)

	for {
		eventlist &lt;- test
		fmt.Println(&quot;Hey!&quot;)
	}
}

Since the channel eventlist is a buffered channel, I think I should get at exactly 100 times the output "Hey!", but it is displayed only once. Where is my mistake?

答案1

得分: 23

更新(Go版本1.2+)

从Go 1.2开始,调度器采用抢占式多任务处理的原则工作。
这意味着原始问题(以及下面提出的解决方案)在Go 1.2中已不再相关。

根据Go 1.2发布说明
> 调度器中的抢占
>
> 在之前的版本中,一个无限循环的goroutine可能会使同一线程上的其他goroutine饿死,
> 当GOMAXPROCS只提供一个用户线程时,这是一个严重的问题。
> 在Go > 1.2中,这个问题在一定程度上得到了解决:调度器在进入函数时会偶尔被调用。
> 这意味着任何包含(非内联)函数调用的循环都可以被抢占,从而允许其他goroutine在同一线程上运行。

简短回答

它不会在写入时阻塞。它被卡在processevents的无限循环中。
这个循环从不让出调度器,导致所有的goroutine无限期地锁定。

如果注释掉对processevents的调用,你将会得到预期的结果,直到第100次写入。此时程序会发生恐慌,因为没有人从通道中读取。

另一种解决方法是在循环中调用runtime.Gosched()

长篇回答

在Go1.0.2中,Go的调度器采用合作式多任务处理的原则工作。
这意味着它通过让这些例程在特定条件下与调度器交互,为在给定的操作系统线程内运行的各种goroutine分配CPU时间。
这些“交互”发生在在goroutine中执行某些类型的代码时。
在Go的情况下,这涉及进行某种I/O、系统调用或内存分配(在某些条件下)。

在空循环的情况下,永远不会遇到这些条件。因此,只要该循环运行,调度器就不会运行其调度算法。这导致它无法为其他等待运行的goroutine分配CPU时间,从而产生了你观察到的结果:你实际上创建了一个无法被调度器检测或打破的死锁。

在Go中通常不希望出现空循环,并且在大多数情况下,这表示程序中存在错误。如果出于任何原因确实需要它,你必须通过在每次迭代中调用runtime.Gosched()来手动让出调度器。

for {
    runtime.Gosched()
}

GOMAXPROCS设置为大于1的值被提到作为一种解决方法。虽然这将消除你观察到的直接问题,但如果调度器决定将循环的goroutine移动到自己的操作系统线程中,它实际上将问题转移到了不同的操作系统线程上。除非在processevents函数的开头调用runtime.LockOSThread(),否则不能保证这一点。即使如此,我仍然不会依赖这种方法作为一个好的解决方案。只需在循环本身中调用runtime.Gosched(),将解决所有问题,无论goroutine在哪个操作系统线程中运行。

英文:

Update (Go version 1.2+)

As of Go 1.2, the scheduler works on the principle of pre-emptive multitasking.
This means that the problem in the original question (and the solution presented below) are no longer relevant.

From the Go 1.2 release notes
> Pre-emption in the scheduler
>
> 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.
> In Go > 1.2, this is partially addressed: The scheduler is invoked occasionally upon
> entry to a function. 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.

Short answer

It is not blocking on the writes. It is stuck in the infinite loop of processevents.
This loop never yields to the scheduler, causing all goroutines to lock indefinitely.

If you comment out the call to processevents, you will get results as expected, right until the 100th write. At which point the program panics, because nobody reads from the channel.

Another solution is to put a call to runtime.Gosched() in the loop.

Long answer

With Go1.0.2, Go's scheduler works on the principle of Cooperative multitasking.
This means that it allocates CPU time to the various goroutines running within a given OS thread by having these routines interact with the scheduler in certain conditions.
These 'interactions' occur when certain types of code are executed in a goroutine.
In go's case this involves doing some kind of I/O, syscalls or memory allocation (in certain conditions).

In the case of an empty loop, no such conditions are ever encountered. The scheduler is therefore never allowed to run its scheduling algorithms for as long as that loop is running. This consequently prevents it from allotting CPU time to other goroutines waiting to be run and the result you observed ensues: You effectively created a deadlock that can not be detected or broken out of by the scheduler.

The empty loop is usually never desired in Go and will, in most cases, indicate a bug in the program. If you do need it for whatever reason, you have to manually yield to the scheduler by calling runtime.Gosched() in every iteration.

for {
    runtime.Gosched()
}

Setting GOMAXPROCS to a value &gt; 1 was mentioned as a solution. While this will get rid of the immediate problem you observed, it will effectively move the problem to a different OS thread, if the scheduler decides to move the looping goroutine to its own OS thread that is. There is no guarantee of this, unless you call runtime.LockOSThread() at the start of the processevents function. Even then, I would still not rely on this approach to be a good solution. Simply calling runtime.Gosched() in the loop itself, will solve all the issues, regardless of which OS thread the goroutine is running in.

答案2

得分: 9

这里是另一种解决方案 - 使用range从通道中读取。这段代码将正确地让出给调度器,并在通道关闭时正确终止。

func processevents(list chan func()) {
    for a := range list{
        a()
    }
}
英文:

Here is another solution - use range to read from the channel. This code will yield to the scheduler correctly and also terminate properly when the channel is closed.

func processevents(list chan func()) {
    for a := range list{
        a()
    }
}

答案3

得分: 1

好消息,自从Go 1.2(2013年12月)以来,原始程序现在按预期工作。
您可以在Playground上尝试它

这在Go 1.2发布说明的“调度器中的抢占”部分中有解释:
> 在之前的版本中,一个无限循环的goroutine可能会使同一线程上的其他goroutine饿死,当GOMAXPROCS只提供一个用户线程时,这是一个严重的问题。在Go 1.2中,这部分得到了解决:调度器在进入函数时偶尔被调用。

英文:

Good news, since Go 1.2 (december 2013) the original program now works as expected.
You may try it on Playground.

This is explained in the Go 1.2 release notes, section "Pre-emption in the scheduler" :
> 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. In Go 1.2, this is partially
> addressed: The scheduler is invoked occasionally upon entry to a
> function.

huangapple
  • 本文由 发表于 2012年9月14日 03:33:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/12413510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定