I'm using a ConcurrentQueue
to enqueue items from an I/O bound task and dequeue them from another for processing. I stop adding items to the queue when it reaches a certain size, so the processing can catch up. To do this I check the ConcurrentQueue.Count
property.
The problem is that the Count
property doesn't seem to behave as it does in a List or other collections. It's extremely slow, and the larger the queue the slower it takes to read the Count
property. With 20k items in a ConcurrentQueue
almost all processor time is spent on the Count
property.
Rough Example:
while (reader.Read())
{
if(Queue.Count >= MaxQueueSize)
{
//Wait
}
//Do Stuff
}
When running a performance profiler, all the time is spent on System.Collections.Concurrent.CDSCollectionETWBCLProvicer.ctor()
.
This only seems to occur on .NET Core 2, this does not occur in .NET 4.6.2
Is there any way around this in .Net Core?