6

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.

Xodarap
  • 11,581
  • 11
  • 56
  • 94
  • Just an idea, could each object type have it's own lock object, so you can `Monitor.Pulse` the appropriate object? – C.Evenhuis Dec 16 '10 at 16:00
  • @deltreme: yeah, that's possible, but I don't want to make a new object for each type. – Xodarap Dec 16 '10 at 16:02
  • I'm not sure what you mean by there being too many types to make separate queues for each. How so? It's the natural way to do it, and I'm not sure how having lots of separate queues would be less efficient than any other event-handling mechanism. –  Dec 16 '10 at 16:10
  • @Isaac: A user says "I want X." X involves aggregating some data in such a way that producer/consumer makes sense. However, no one may ever request X again, so having a queue just for X is pointless, and may be impossible since we cannot predict what the user wants. You may consider it equivalent to how events take parameters - even though there is one generic type of event ("producer made something"), we want to handle that event differently. – Xodarap Dec 16 '10 at 16:20
  • @Isaac: I have added an example; it might make it more clear what I want. It is very probable that I am approaching the problem wrong. – Xodarap Dec 16 '10 at 16:33
  • 1
    How about this: Whenever there is a pulse, it will activate next consumer to wake up from sleep. Now, at this moment he can `seek` what is there to use, and if its not his type, he can go back to sleep and let other guy take his turn. If this does not affect any performance, I thinks it is a simple approach. – decyclone Dec 16 '10 at 17:26

2 Answers2

3

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.

class MyWorker 
{
    public string MyType {get; set;}
    public static PriorityBlockingQueue<string, MyInfo> data; 

    public static void DoWork()
    {
        while(true)
        {
            MyInfo value;
            if (data.TryTake(MyType, timeout, out value))
            {
                // OK, do work
            }
        }
    }
}

Implementing a PriorityBlockingQueue is not terribly difficult. Following the same pattern as BlockingCollection by utilizing Add and Take style methods I came up with the following code.

public class PriorityBlockingQueue<TKey, TValue>
{
    private SortedDictionary<TKey, TValue> m_Dictionary = new SortedDictionary<TKey,TValue>();

    public void Add(TKey key, TValue value)
    {
        lock (m_Dictionary)
        {
            m_Dictionary.Add(key, value);
            Monitor.Pulse(m_Dictionary);
        }
    }

    public TValue Take(TKey key)
    {
        TValue value;
        TryTake(key, TimeSpan.FromTicks(long.MaxValue), out value);
        return value;
    }

    public bool TryTake(TKey key, TimeSpan timeout, out TValue value)
    {
        value = default(TValue);
        DateTime initial = DateTime.UtcNow;
        lock (m_Dictionary)
        {
            while (!m_Dictionary.TryGetValue(key, out value))
            {
                if (m_Dictionary.Count > 0) Monitor.Pulse(m_Dictionary); // Important!
                TimeSpan span = timeout - (DateTime.UtcNow - initial);
                if (!Monitor.Wait(m_Dictionary, span))
                {
                    return false;
                }
            }
            m_Dictionary.Remove(key);
            return true;
        }
    }
}

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 the TryTake method 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 a SortedDictionary instead despite its inferior performance in this scenario.

I should point out here that this does not actually solve the pointless Wait/Pulse behavior. It is simply encapsulated in the PriorityBlockingQueue class. 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 old MyInfo when adding to the dictionary.

Brian Gideon
  • 47,849
  • 13
  • 107
  • 150
  • Interesting, good idea to encapsulate this. Is there a reason you used `SortedDictionary` instead of just `Dictionary`? Just quicker insert times? – Xodarap Dec 16 '10 at 20:28
  • @Xodarap: Well, good question. I was originally thinking that you would need a `Take` overload that did not accept a key and it would remove the first item out of the dictionary. You need a sorted dictionary to do that correctly though. That is actually how a priority queue is suppose to work. I suppose you could use a plain old dictionary and just call the wrapper class `BlockingDictionary` instead. Nice catch. – Brian Gideon Dec 16 '10 at 22:00
1

It seems like you want to combine producer/consumer queue with an Observer pattern - generic consumer thread or threads reads from the queue, and then passes the event to the required code. In this instance you would not actually signal the Observer but just call it when the consumer thread identifies who is interested in a given work item.

Observer pattern in .Net is typically implemented using C# events. You would just need to call the event handler for the object and one or more observers would get invoked through it. The target code would first have to register itself with the observed object by adding itself to the event for notification on arrival of work.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
  • This sounds like I should have a `Dictionary` and just do `Monitor.Pulse(Dictionary[type])` when `type` is updated? It seems like I can remove the observer and just do this manually from the producer then. (I agree it will work, but I was hoping that I could use a behind-the-scenes collection of consumers.) – Xodarap Dec 16 '10 at 17:02
  • If you are using `Monitor` to active the target object then having a separate consumer thread accessed via queue seems redundant, for sure. – Steve Townsend Dec 16 '10 at 19:13