Thinking about it, use the expected count, in the done process decrement the 
count and only print when 0. Thus works if the channels are unbuffered. 


> On Dec 18, 2018, at 7:49 AM, Robert Engels <reng...@ix.netcom.com> wrote:
> 
> This is not trivial, as you are imposing an order on unrelated async events. 
> 
> Would you phrase the condition as, do not process a done until I’ve processed 
> requests for N routines - then you are back to using an expected count. 
> 
> With an expected count, the problem is trivial.
> 
> I think you need to step back and decide what you are trying to parallelize.
> 
>> On Dec 18, 2018, at 7:34 AM, Chris Burkert <burkert.ch...@gmail.com> 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.

-- 
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