I have a producer / consumer queue, except that there are specific types of objects. So not just any consumer can consume an added object. I don’t want to make a specific queue for each type, as there are too many. (It sort of stretches the definition of producer/consumer, but I’m not sure what the correct term is.)
Is there such a thing as an EventWaitHandle which allows pulses with a parameter? e.g. myHandle.Set(AddedType = "foo"). Right now I’m using Monitor.Wait and then each consumer checks to see if the pulse was actually intended for them, but that seems kind of pointless.
A pseduocode version of what I have now:
class MyWorker {
public string MyType {get; set;}
public static Dictionary<string, MyInfo> data;
public static void DoWork(){
while(true){
if(Monitor.Wait(data, timeout)){
if (data.ContainsKey(MyType)){
// OK, do work
}
}
}
}
}
As you can see, I might get pulses when other stuff is added to the dict. I only care when MyType is added to the dict. Is there a way to do that? It’s not a huge deal, but, for example, I have to manually handle timeouts now, because each get of the lock could succeed within the timeout, but MyType is never added to the dict within timeout.
This is an interesting question. It sounds like the key to the solution is a blocking variant of a priority queue. Java has the
PriorityBlockingQueue, but unfortunately the equivalent for the .NET BCL is nonexistent. Once you have one, however, the implementation is easy.Implementing a
PriorityBlockingQueueis not terribly difficult. Following the same pattern asBlockingCollectionby utilizingAddandTakestyle methods I came up with the following code.This was a quick implementation and it has a couple of problems. First, I have not tested it at all. Second, it uses a red-black tree (via
SortedDictionary) as the underlying data structure. That means theTryTakemethod will have O(log(n)) complexity. Priority queues typically have O(1) removal complexity. The typically data structure of choice for priority queues is a heap, but I find that skip lists are actually better in practice for several reasons. Neither of these exist in the .NET BCL which is why I used aSortedDictionaryinstead despite its inferior performance in this scenario.I should point out here that this does not actually solve the pointless
Wait/Pulsebehavior. It is simply encapsulated in thePriorityBlockingQueueclass. But, at the very least this will certainly cleanup the core part of your code.It did not appear like your code handled multiple objects per key, but that would be easy to add by using a
Queue<MyInfo>instead of a plain oldMyInfowhen adding to the dictionary.