IAsyncEnumerable<T> for AsyncProducerConsumerQueue<T>
I currently use this approach and it works fine
var items = new AsyncProducerConsumerQueue<int>();
while (await items.OutputAvailableAsync())
{
var item = await items.DequeueAsync();
}
I was wondering if it's possible to have an IAsyncEnumerable<T> method on AsyncProducerConsumerQueue<T> so that I can do this instead.
await foreach (var item in items.DequeueItemsAsync()) { }
My only qualms with the the OutputAvailable/Dequeue approach is that in between the 2 methods, another thread could theoretically empty the queue. Anyway, thank you for this awesome library.
Yes, certainly. It could use an update to support async enumerables.
In the meantime, you may want to check out Channels. They are the most modern implementation of an async producer/consumer queue.
Provocative thoughts: Is there any functionality which AsyncProducerConsumerQueue<T> provides on top of what Channels provide? Should AsyncProducerConsumerQueue<T> be deprecated in favour of channels?
Oh, that's not provocative at all. :)
For the last couple of years, I've been steering everyone I can towards Channels. I wouldn't say AsyncProducerConsumerQueue<T> is deprecated (it's still supported), but Channels is faster and has full support for modern techniques like async streams. I mostly use Channels myself these days.
@StephenCleary thanks for the advice about Channels, it's exactly what I was looking for.