ConcurrentQueue Not Always Dequeueing what was Successfully Enqueued

Dave DeBenedetto 96 Reputation points
2021-03-31T17:58:58.147+00:00

Hello,
I have a C# Windows service which has been running in my production environment for years.

The service has several ConcurrentQueue instances. One of these queues (queue "A") receives Enqueues from three different "producers", each one within a different thread.

A fourth thread does the Dequeuing.

I’ve made some seemingly unrelated code changes, and am perplexed by what I'm now observing: Queue "A" only correctly processes traffic from two of the three producers.

While debugging, I stopped the Enqueues from the two "working" producers, and observed the following with the "problem producer":

  1. When I step through the Enqueue statement, everything looks fine: That is, the Queue.Count correctly increments, Queue.IsEmpty == false, and my data is correctly residing in a queue element.
  2. However, when the fourth thread calls TryDequeue on the same queue, nothing is found. Also, Queue.IsEmpty == true, and Queue.Count == 0.
  3. Repeated Enqueues cause the Queue.Count to keep increasing and my new items accumulating, as shown by the debugger.

Has anyone ever encountered this type of behavior?
Thanks in advance,
Dave

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,233 questions
{count} votes

Accepted answer
  1. Dave DeBenedetto 96 Reputation points
    2021-04-05T18:10:03.967+00:00

    The problem has been solved.

    The cause was banal: To code a new class instantiation, I copied and modified an existing code block. But in doing so, I accidentally copied more than I meant to, inadvertently instantiating the class that creates the concurrent queues twice.

    0 comments No comments

0 additional answers

Sort by: Most helpful