英文:
why golang select statement does not randomly choose a case
问题
这类似于https://stackoverflow.com/questions/34931059/go-tutorial-select-statement,但我没有从那篇帖子中得到答案。所以我在这里问。谢谢你的回答。
在http://tour.golang.org/concurrency/5中,似乎"case c <- x:"总是准备好的,这意味着这个case不会阻塞select语句。
根据"select会阻塞,直到其中一个case可以运行,然后执行该case。如果有多个case准备好,它会随机选择一个。"这句话,当"case <-quit:"也准备好时,select语句应该随机选择"case c <- x:"和"case <-quit:"。但是程序总是进入"case <-quit:"。
我还将select块更改为以下形式。然后在前10次循环中,程序随机打印1-6,但是一旦quit通道的值为0,程序就会退出(第11个输出)。
我的问题是,如果准备好的case是随机选择的,那么为什么第11次选择总是quit case。
select {
case c <- 1:
x, y = y, x+y
case c <- 2:
x, y = y, x+y
case c <- 3:
x, y = y, x+y
case c <- 4:
x, y = y, x+y
case c <- 5:
x, y = y, x+y
case c <- 6:
x, y = y, x+y
case <-quit:
fmt.Println("quit")
return
}
英文:
This is similar to https://stackoverflow.com/questions/34931059/go-tutorial-select-statement, but I didn't get answer from that post. So I asked here. Thanks for answering.
In http://tour.golang.org/concurrency/5, it seems "case c <- x:" is always ready, which means this case will not block select statement.
Based on the sentence "A select blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.", when "case <-quit:" is also ready, the select statement should randomly choose from "case c <- x:" and "case <-quit:". But the program always go into the "case <-quit:" case.
I also changed the select block to be like below. Then in the first 10 loops, the program randomly print 1-6, but the the program quits once (the 11th output) the quit channel has value 0.
My question is that if the ready cases are randomly selected from, then why the 11th selection is alway the quit case.
select {
case c <- 1:
x, y = y, x+y
case c <- 2:
x, y = y, x+y
case c <- 3:
x, y = y, x+y
case c <- 4:
x, y = y, x+y
case c <- 5:
x, y = y, x+y
case c <- 6:
x, y = y, x+y
case <-quit:
fmt.Println("quit")
return
}
答案1
得分: 5
在case
语句中,你将值发送到c
(例如c <- 1
),它会阻塞直到有其他地方读取c
的值,例如foo := <- c
。当有东西写入quit
时,它会命中<-quit
所在的case
语句,并从select
语句中返回。
从这个例子中,你可以看到在你的机器上随机打印出的值为:
$ go run foo.go
Bar 0
Bar 1
Foo 2
Bar 3
quit
$ go run foo.go
Bar 0
Foo 1
Bar 2
Bar 3
quit
英文:
On the case statements, you're sending values to c
(e.g c <- 1
) which blocks until something reads off c as foo := <- c
. When something writes to quit
it will hit the case where <-quit
is at and returning out of the select.
From this example
package main
import (
"fmt"
"time"
)
func main() {
c := make(chan int)
quit := make(chan struct{})
go func(q chan<- struct{}) {
time.Sleep(5 * time.Second)
q <- struct{}{}
}(quit)
go func(ch chan<- int) {
var x int
for range time.Tick(1 * time.Second) {
c <- x
x++
}
}(c)
for {
select {
case foo := <-c:
fmt.Println("Foo", foo)
case bar := <-c:
fmt.Println("Bar", bar)
case <-quit:
fmt.Println("quit")
return
}
}
}
You can see the values randomly printed out on your machine as:
$ go run foo.go
Bar 0
Bar 1
Foo 2
Bar 3
quit
$ go run foo.go
Bar 0
Foo 1
Bar 2
Bar 3
quit
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论