Go在休眠时会超时,但在忙等待时不会超时。

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

Go times out on sleep but not on busy-wait

问题

在Go语言中,可以使用time.After函数来设置一个睡眠函数的超时时间,但是对于一个忙等待(或工作)的函数,无法使用相同的方法。以下代码在一秒钟后返回timed out,然后进入无限等待状态。

package main

import (
	"fmt"
	"time"
)

func main() {
	sleepChan := make(chan int)
	go sleep(sleepChan)
	select {
	case sleepResult := <-sleepChan:
		fmt.Println(sleepResult)
	case <-time.After(time.Second):
		fmt.Println("timed out")
	}

	busyChan := make(chan int)
	go busyWait(busyChan)
	select {
	case busyResult := <-busyChan:
		fmt.Println(busyResult)
	case <-time.After(time.Second):
		fmt.Println("timed out")
	}
}

func sleep(c chan<- int) {
	time.Sleep(10 * time.Second)
	c <- 0
}

func busyWait(c chan<- int) {
	for {
	}
	c <- 0
}

为什么第二个case中的超时没有触发,以及我需要使用什么替代方法来中断正在工作的goroutine呢?

英文:

In Go, I can use time.After to time out a sleeping function, but I can't do the same to a function that is busy-waiting (or working). The following code returns timed out after one second, and then hangs.

package main

import (
        &quot;fmt&quot;
        &quot;time&quot;
)

func main() {
        sleepChan := make(chan int)
        go sleep(sleepChan)
        select {
        case sleepResult := &lt;-sleepChan:
                fmt.Println(sleepResult)
        case &lt;-time.After(time.Second):
                fmt.Println(&quot;timed out&quot;)
        }

        busyChan := make(chan int)
        go busyWait(busyChan)
        select {
        case busyResult := &lt;-busyChan:
                fmt.Println(busyResult)
        case &lt;-time.After(time.Second):
                fmt.Println(&quot;timed out&quot;)
        }
}

func sleep(c chan&lt;- int) {
        time.Sleep(10 * time.Second)
        c &lt;- 0
}

func busyWait(c chan&lt;- int) {
        for {
        }
        c &lt;- 0
}

Why doesn't the timeout fire in the second case, and what alternative do I need to use to interrupt working goroutines?

答案1

得分: 6

for {}语句是一个无限循环,它会独占一个处理器。将runtime.GOMAXPROCS设置为2或更多以允许计时器运行。

例如,

package main

import (
	"fmt"
	"runtime"
	"time"
)

func main() {
	fmt.Println(runtime.GOMAXPROCS(0))
	runtime.GOMAXPROCS(runtime.NumCPU())
	fmt.Println(runtime.GOMAXPROCS(0))
	sleepChan := make(chan int)
	go sleep(sleepChan)
	select {
	case sleepResult := <-sleepChan:
		fmt.Println(sleepResult)
	case <-time.After(time.Second):
		fmt.Println("timed out")
	}

	busyChan := make(chan int)
	go busyWait(busyChan)
	select {
	case busyResult := <-busyChan:
		fmt.Println(busyResult)
	case <-time.After(time.Second):
		fmt.Println("timed out")
	}
}

func sleep(c chan<- int) {
	time.Sleep(10 * time.Second)
	c <- 0
}

func busyWait(c chan<- int) {
	for {
	}
	c <- 0
}

输出(4个CPU处理器):

1
4
timed out
timed out
英文:

The for {} statement is an infinite loop which monopolizes a single processor. Set runtime.GOMAXPROCS to 2 or more to allow the timer to run.

For example,

package main

import (
	&quot;fmt&quot;
	&quot;runtime&quot;
	&quot;time&quot;
)

func main() {
	fmt.Println(runtime.GOMAXPROCS(0))
	runtime.GOMAXPROCS(runtime.NumCPU())
	fmt.Println(runtime.GOMAXPROCS(0))
	sleepChan := make(chan int)
	go sleep(sleepChan)
	select {
	case sleepResult := &lt;-sleepChan:
		fmt.Println(sleepResult)
	case &lt;-time.After(time.Second):
		fmt.Println(&quot;timed out&quot;)
	}

	busyChan := make(chan int)
	go busyWait(busyChan)
	select {
	case busyResult := &lt;-busyChan:
		fmt.Println(busyResult)
	case &lt;-time.After(time.Second):
		fmt.Println(&quot;timed out&quot;)
	}
}

func sleep(c chan&lt;- int) {
	time.Sleep(10 * time.Second)
	c &lt;- 0
}

func busyWait(c chan&lt;- int) {
	for {
	}
	c &lt;- 0
}

Output (4 CPU processor):

1
4
timed out
timed out

huangapple
  • 本文由 发表于 2014年3月8日 12:57:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/22265245.html
匿名

发表评论

匿名网友

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

确定