Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Difference between Queue's and Concurrent Queue's TryDequeue method

Both collections, Queue and ConcurrentQueue have a method TryDequeue. What is the difference between using TryDequeue with Queue and ConcurrentQueue respectively? Is Queue’s TryDequeue method thread-safe in multi-threading environment?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Queue.TryDequeue() is not threadsafe.

We can look at its implementation for proof:

public bool TryDequeue([MaybeNullWhen(false)] out T result)
{
    int head = _head;
    T[] array = _array;

    if (_size == 0)
    {
        result = default;
        return false;
    }

    result = array[head];
    if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
    {
        array[head] = default!;
    }
    MoveNext(ref _head);
    _size--;
    _version++;
    return true;
}

It’s easy to see that isn’t threadsafe. Just the _size-- alone is not threadsafe.

But even without the source code, the documentation for Queue<T> explicitly states:

Any instance members are not guaranteed to be threadsafe.

Of course, the methods of ConcurrentQueue are threadsafe, and by definition ImmutableQueue is also threadsafe.

(The Try in the name of TryDequeue() is referring to the fact that it handles an empty queue rather than anything to do with thread safety.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading