Why doesn't ConcurrentQueue
have a capacity
like its non-concurrent cousin?
There isn't a mentioning of a default capacity either.
Does the "missing" capacity impact performance compared to the non-concurrent version where the implementer can provide a qualified guess of the typical size of the queue?
ConcurrentQueue is implemented using lock free techniques. It is based on "Linked list". Capacity doesn't makes sense in Linked list so it doesn't expose the capacity.
Adding elements to the LinkedList is dirt cheap, it doesn't need to resize the array. It is just modifying the Tail pointer(reference). If the implementation uses array, then resizing the array is expensive, so pre allocating the array will improve performance significantly when you enqueue many elements.
Other non concurrent implementations of the Queue
(System.Collections.Generic.Queue<T>
and System.Collections.Queue
) are array based. So it makes sense to pre allocate the array with given capacity in order to avoid resizing the array often and thus it exposes a Capacity property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With