为什么 Golang 的 select 语句不会随机选择一个 case?

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

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。

  1. select {
  2. case c &lt;- 1:
  3. x, y = y, x+y
  4. case c &lt;- 2:
  5. x, y = y, x+y
  6. case c &lt;- 3:
  7. x, y = y, x+y
  8. case c &lt;- 4:
  9. x, y = y, x+y
  10. case c &lt;- 5:
  11. x, y = y, x+y
  12. case c &lt;- 6:
  13. x, y = y, x+y
  14. case &lt;-quit:
  15. fmt.Println(&quot;quit&quot;)
  16. return
  17. }
英文:

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.

  1. select {
  2. case c &lt;- 1:
  3. x, y = y, x+y
  4. case c &lt;- 2:
  5. x, y = y, x+y
  6. case c &lt;- 3:
  7. x, y = y, x+y
  8. case c &lt;- 4:
  9. x, y = y, x+y
  10. case c &lt;- 5:
  11. x, y = y, x+y
  12. case c &lt;- 6:
  13. x, y = y, x+y
  14. case &lt;-quit:
  15. fmt.Println(&quot;quit&quot;)
  16. return
  17. }

答案1

得分: 5

case语句中,你将值发送到c(例如c <- 1),它会阻塞直到有其他地方读取c的值,例如foo := <- c。当有东西写入quit时,它会命中<-quit所在的case语句,并从select语句中返回。

从这个例子中,你可以看到在你的机器上随机打印出的值为:

  1. $ go run foo.go
  2. Bar 0
  3. Bar 1
  4. Foo 2
  5. Bar 3
  6. quit
  7. $ go run foo.go
  8. Bar 0
  9. Foo 1
  10. Bar 2
  11. Bar 3
  12. quit
英文:

On the case statements, you're sending values to c (e.g c &lt;- 1) which blocks until something reads off c as foo := &lt;- c. When something writes to quit it will hit the case where &lt;-quit is at and returning out of the select.

From this example

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;time&quot;
  5. )
  6. func main() {
  7. c := make(chan int)
  8. quit := make(chan struct{})
  9. go func(q chan&lt;- struct{}) {
  10. time.Sleep(5 * time.Second)
  11. q &lt;- struct{}{}
  12. }(quit)
  13. go func(ch chan&lt;- int) {
  14. var x int
  15. for range time.Tick(1 * time.Second) {
  16. c &lt;- x
  17. x++
  18. }
  19. }(c)
  20. for {
  21. select {
  22. case foo := &lt;-c:
  23. fmt.Println(&quot;Foo&quot;, foo)
  24. case bar := &lt;-c:
  25. fmt.Println(&quot;Bar&quot;, bar)
  26. case &lt;-quit:
  27. fmt.Println(&quot;quit&quot;)
  28. return
  29. }
  30. }
  31. }

You can see the values randomly printed out on your machine as:

  1. $ go run foo.go
  2. Bar 0
  3. Bar 1
  4. Foo 2
  5. Bar 3
  6. quit
  7. $ go run foo.go
  8. Bar 0
  9. Foo 1
  10. Bar 2
  11. Bar 3
  12. quit

huangapple
  • 本文由 发表于 2016年3月31日 22:07:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/36335779.html
匿名

发表评论

匿名网友

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

确定