关于通道的顺序

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

About the order of channels

问题

我运行了下面的代码多次,有时在“test2”之后会打印出“test”。为什么会这样呢?我认为“true”首先被发送到“test”。

package main

import "log"
import "time"

func main() {
    test := make(chan bool, 1)
    test2 := make(chan bool, 1)

    go func() {
        for {
            select {
            case <-test:
                log.Println("test")
            case <-test2:
                log.Println("test2")
            }
        }
    }()

    time.Sleep(3 * time.Second)
    test <- true
    test2 <- true
    time.Sleep(1)
}

编辑于2014/7/27:
阅读了这篇文章后,我认为每个"test <- true"和"test2 <- true"都可以是"happens-before"、"happens-after"或者"happens-concurrently"。

英文:

I ran the code below several times and sometimes "test" was printed after "test2".<br>
Why is that?<br>
I think "true" is sent to "test" first.<br>

package main

import &quot;log&quot;
import &quot;time&quot;

func main() {
	test := make(chan bool, 1)
	test2 := make(chan bool, 1)

	go func() {
		for {
			select {
			case &lt;-test:
				log.Println(&quot;test&quot;)
			case &lt;-test2:
				log.Println(&quot;test2&quot;)
			}
		}
	}()

    time.Sleep(3 * time.Second)
	test &lt;- true
	test2 &lt;- true
	time.Sleep(1)
}

Edited 2014/7/27:<br>
After reading the article, I think that each of "test <- true" and "test2 <- true" could be "happens-before" or "happens-after" or also "happens-concurrently".

答案1

得分: 1

如果可以进行一个或多个通信,则通过均匀伪随机选择选择一个可以进行的通信。您正在看到一个均匀伪随机选择。

英文:

"If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection." You are seeing a uniform pseudo-random selection.

> The Go Programming Language Specification
>
> Select statements
>
> Execution of a "select" statement proceeds in several steps:
>
> 1) For all the cases in the statement, the channel operands of receive
> operations and the channel and right-hand-side expressions of send
> statements are evaluated exactly once, in source order, upon entering
> the "select" statement. The result is a set of channels to receive
> from or send to, and the corresponding values to send. Any side
> effects in that evaluation will occur irrespective of which (if any)
> communication operation is selected to proceed. Expressions on the
> left-hand side of a RecvStmt with a short variable declaration or
> assignment are not yet evaluated.
>
> 2) If one or more of the communications can proceed, a single one that
> can proceed is chosen via a uniform pseudo-random selection.
> Otherwise, if there is a default case, that case is chosen. If there
> is no default case, the "select" statement blocks until at least one
> of the communications can proceed.
>
> 3) Unless the selected case is the default case, the respective
> communication operation is executed.
>
> 4) If the selected case is a RecvStmt with a short variable
> declaration or an assignment, the left-hand side expressions are
> evaluated and the received value (or values) are assigned.
>
> 5) The statement list of the selected case is executed.
>
> Since communication on nil channels can never proceed, a select with
> only nil channels and no default case blocks forever.

huangapple
  • 本文由 发表于 2014年7月27日 02:12:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/24974055.html
匿名

发表评论

匿名网友

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

确定