Home > Enterprise >  Difference between Queue's and Concurrent Queue's TryDequeue method
Difference between Queue's and Concurrent Queue's TryDequeue method

Time:10-18

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?

CodePudding user response:

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.)

CodePudding user response:

Nothing on Queue<T> is thread-safe - not even Count

The difference is entirely: thread-safety. On ConcurrentQueue<T>, yes: it is thread-safe. There are also some minor API differences (the lack of Dequeue() on the concurrent version, for example), but mostly: the API shape is directly comparable, give or take thread-safety.

  • Related