The time.After _is_ evaluated each time.

From http://golang.org/ref/spec#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.

On Thu, Jun 25, 2015 at 7:34 PM, Yicheng Qin wrote:

Got it. So i will make it wait longer to pass every time.

The long delay starts to happen when we set --cpu > 1. It happens once per
100 tests, so the probability is 1 out of 1 million times.

It confused me most due to its order. When it comes to the select
statement, the `ch` has been closed, and `time.After` is not called. But it
goes into the `time.After` case finally. I guess its internal
implementation is to 1. execute `time.After` 2. wait for two channels 3.
select one from ready channels randomly.
On Thu, Jun 25, 2015 at 10:11 AM, Russ Cox wrote:

In this code:

select {
case <-ch:
case <-time.After(10 * time.Millisecond):

If the second case triggers, it does not imply that the channel is not
closed (obviously it is closed, since you closed it before executing the
select). It means that in the time between time.After setting the timer for
its channel and the time that select looked at the channel, the timer went
off. As Brad speculated, it could be garbage collection. It could also be
that your process was interrupted by the operating system so that some
other process could run. There are many possible reasons. I would not worry
about it happening once in a while. It might be more interesting to check
how many times it happens out of the 10000.

You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 6 of 7 | next ›
Discussion Overview
groupgolang-nuts @
postedJun 24, '15 at 9:27p
activeJun 25, '15 at 10:13p



site design / logo © 2021 Grokbase