Is this BlockingQueue susceptible to deadlock?

I used this code as a queue that locks on Dequeue()until an item is queued. I have used this code for several years in several projects, no problem ... so far. I see a dead end in some code that I am writing now, and, exploring the problem, my “eye of suspicion” stopped there BlockingQueue<T>. I can’t prove it, so I thought I would ask some people smarter than me to review it for potential problems. Can you guys see anything that could cause a dead end in this code?

public class BlockingQueue<T>
{
    private readonly Queue<T> _queue;
    private readonly ManualResetEvent _event;

    /// <summary>
    /// Constructor
    /// </summary>
    public BlockingQueue()
    {
        _queue = new Queue<T>();
        _event = new ManualResetEvent(false);
    }

    /// <summary>
    /// Read-only property to get the size of the queue
    /// </summary>
    public int Size
    {
        get
        {
            int count;

            lock (_queue)
            {
                count = _queue.Count;
            }

            return count;
        }
    }

    /// <summary>
    /// Enqueues element on the queue
    /// </summary>
    /// <param name="element">Element to enqueue</param>
    public void Enqueue(T element)
    {
        lock (_queue)
        {
            _queue.Enqueue(element);
            _event.Set();
        }
    }

    /// <summary>
    /// Dequeues an element from the queue
    /// </summary>
    /// <returns>Dequeued element</returns>
    public T Dequeue()
    {
        T element;

        while (true)
        {
            if (Size == 0)
            {
                _event.Reset();
                _event.WaitOne();
            }

            lock (_queue)
            {
                if (_queue.Count == 0) continue;

                element = _queue.Dequeue();
                break;
            }
        }

        return element;
    }

    /// <summary>
    /// Clears the queue
    /// </summary>
    public void Clear()
    {
        lock (_queue)
        {
            _queue.Clear();
        }
    }
}
+3
source share
3

, :

Thread 1                    Thread 2
Dequeue
                            Enqueue    
if (Size == 0)                              // Thread 1 gets the lock
                            lock (_queue)   // Thread 2 has to wait
return _queue.Count                         // Thread 1 sees: Size == 0
                            _queue.Enqueue  // Thread 2 gets the lock
                            _event.Set    
_event.Reset                                // uh oh
_event.WaitOne                              // now Dequeue going to block
                                            // until Enqueue gets called again
                                            // (even though queue isn't empty)
+7

. . if (Size == 0) _event.Reset(). , .

Queue .

+1

I do not know your requirements or what else your class does, but if you can use .NET 4, you can consider using ConcurrentQueue<T>and BlockingCollection<T>, which are used together, should give you a blocking queue.

0
source

Source: https://habr.com/ru/post/1744508/


All Articles