I have one producer and multiple consumers. I am using ConcurrentQueue
. I am using C# but I think my problem is language agnostics.
There can be non-unique consumers. i.e. more than one consumers can be interested in same message. So, non-unique consumers are like workers-of-one-kind.
There are unique consumers as well and since they are dealing with a queue, they assume ordering in messages.
Problem:
When a consumer Guid-1-worker-1
peek into the queue and finds a message M1 for itself, it will dequeue it. However, because of concurrency, message M1 might have been dequeued by worker Guid-1-worker-2
. At this time, message dequeued by Guid-1-worker-1
is for Guid-2
. Guid-2
has only one worker and hence, putting the message back in queue does not help as it breaks the ordering of messages.
What I want is that if I have looked at a message, then only i should be able to dequeue it. Now, I think I have to use lock
here. But simple List
will work instead of ConcurrentQueue
as well.
Is there any other data structure that provides : Peek-Check-Dequeue
kind of concurrent semantics ?
Is there any other way to model the problem ?