You are both correct, I somehow missed that they were buffered. Thanks for
correcting me. On a side note, is there a reason not to use a wait all?
Thanks.

On Tue, Dec 18, 2018 at 10:05 AM Robert Engels <reng...@ix.netcom.com>
wrote:

> I don’t think that code is correct. You are using buffered channels do it
> could read the done values and exit before reading any results technically
> since when data is available on both it is random which is read.
>
> On Dec 18, 2018, at 8:32 AM, john_halder...@moma.org wrote:
>
> This code works though, it's not possible to send the dc before the rc is
> read and so the done counter cannot reach 2 before both 0s have been
> written. I made a playground that runs it 100k times to demonstrate.
> https://play.golang.org/p/DKltfzDI95L
>
> On Tuesday, December 18, 2018 at 8:35:26 AM UTC-5, Chris Burkert wrote:
>>
>> Dear all,
>>
>> I have a couple of goroutines sending multiple results over a channel - a
>> simple fan-in. They signal the completion on a done channel. Main selects
>> on the results and done channel in parallel. As the select is random main
>> sometimes misses to select the last result. What would be the idiomatic way
>> to prevent this and completely drain the result channel?
>>
>> Here is a minmal example which sometimes prints one 0 but should always
>> print two of them:
>>
>> package main
>>
>> import (
>>         "fmt"
>> )
>>
>> func do(rc chan<- int, dc chan<- bool) {
>>         rc <- 0
>>         dc <- true
>> }
>>
>> func main() {
>>         worker := 2
>>         rc := make(chan int, worker)
>>         done := 0
>>         dc := make(chan bool, worker)
>>         for i := 0; i < worker; i++ {
>>                 go do(rc, dc)
>>         }
>>         for done < worker {
>>                 select {
>>                 case <-dc:
>>                         done++
>>                 case r := <-rc:
>>                         fmt.Println(r)
>>                 }
>>         }
>> }
>>
>> many thanks
>> Chris
>>
> --
> 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.
> For more options, visit https://groups.google.com/d/optout.
>
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to