Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3606816
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:17:49+00:00 2026-05-18T21:17:49+00:00

I have a producer / consumer queue, except that there are specific types of

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:17:49+00:00Added an answer on May 18, 2026 at 9:17 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project that adds elements to an AutoCad drawing. I noticed that
I have a script that appends some rows to a table. One of the
I have a new web app that is packaged as a WAR as part
I have several USB mass storage flash drives connected to a Ubuntu Linux computer
I have a snippet to create a 'Like' button for our news site: <iframe
I have found this example on StackOverflow: var people = new List<Person> { new
I have a login.jsp page which contains a login form. Once logged in the
i have a input tag which is non editable, but some times i need
I'm in the process of porting some code from Linux to Mac OS X.
I am using a 3rd-party rotator object, which is providing a smooth, random rotation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.