英文:
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 (
"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
}
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 (
"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
}
Output (4 CPU processor):
1
4
timed out
timed out
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论