AsyncEx icon indicating copy to clipboard operation
AsyncEx copied to clipboard

Approximate count for the AsyncProducerConsumerQueue

Open bboyle1234 opened this issue 6 years ago • 2 comments

Can we add an "approximate count" property to the AsyncProducerConsumerQueue? In my usage, the queue often provides a buffer that allows a data source thread to continue operating without waiting for consuming threads to process the data. This brings up the issue of backpressure monitoring, and I would like the app to be able to report the queue Count as an indicator of app performance. The implementation could be as simple as Interlocked.Increment(ref count) when enqueing an item, and decrementing when the item is dequeued. The intention of the property is not for it to be perfectly accurate and synchronized with the reading/writing threads, but for it to give a general idea of how much backpressure exists by the approximate size of the queue.

bboyle1234 avatar Sep 24 '19 00:09 bboyle1234

I'm in the same situation - I need to monitor the number of items in the queue for status reporting. I think it's as simple as adding:

public int Count => _queue.Count;

paulhickman-a365 avatar May 01 '21 21:05 paulhickman-a365

If I add this, it certainly won't be named Count. ;) That would encourage misuse. I'd probably call it ApproximateCount.

StephenCleary avatar May 04 '21 12:05 StephenCleary