On Mon, Dec 19, 2022 at 11:01 AM Torsten Bronger
<bron...@physik.rwth-aachen.de> wrote:

> The context documentation gives this example:
>
>     // Stream generates values with DoSomething and sends them to out
>     // until DoSomething returns an error or ctx.Done is closed.
>     func Stream(ctx context.Context, out chan<- Value) error {
>         for {
>                 v, err := DoSomething(ctx)
>                 if err != nil {
>                         return err
>                 }
>                 select {
>                 case <-ctx.Done():
>                         return ctx.Err()
>                 case out <- v:
>                 }
>         }
>     }
>
> What if the channel “out” is drained very efficiently?  Then, an
> arbitrary number of loop iterations could happen before a
> cancellation is detected, couldn’t it?

>From https://go.dev/ref/spec#Select_statements

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

Selecting the send case in the above code, when both cases can
proceed, for N times in a row, should have on average a probability of
2^(-N).

-j

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAA40n-WRAxoBXU9xb3DhmG%3DxcPr0%3DcxdjwX8gq%2B_%3Dwb_OS443A%40mail.gmail.com.

Reply via email to