Re: [go-nuts] Drain channel on demand without busy looping?

2023-02-05 Thread Jan Mercl
On Sun, Feb 5, 2023 at 5:12 PM cpu...@gmail.com  wrote:

> Would that still busy-loop? Are there better patterns?

for range q.data {}

iff the sending side properly closes the channel.

-- 
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-VAiRkF7PEZnOq50UnOJf%3D-eptD-hJ9WDaBOC0AvnL1zQ%40mail.gmail.com.


[go-nuts] Drain channel on demand without busy looping?

2023-02-05 Thread cpu...@gmail.com
I have the requirement of being able to drain a channel, i.e. make sure 
that downstream processes all data currently being sent. One possible 
pattern is something like

for{
select{
case <-q.data:
default:
}
}

where the default branch could indicate that channel has been drained. My 
concern is, that this would create a busy loop on the default branch even 
when not trying to drain the channel.

I could use an atomic for checking if work should be done in default (i.e. 
signal channel has been drained).

Would that still busy-loop? Are there better patterns?

Cheers,
Andi

-- 
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/4887e28d-bcfb-4f49-9450-00e56e00be55n%40googlegroups.com.