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

  • Home
  • SEARCH
  • 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 579111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:19:55+00:00 2026-05-13T14:19:55+00:00

I need to implement a producer/consumer bounded queue, multiple consumers against a single producer.

  • 0

I need to implement a producer/consumer bounded queue, multiple consumers against a single producer.

I have a push function that adds an item to the queue and then checks for maxsize. If we have reached it return false, in every other case return true.

In the following code _vector is a List<T>, onSignal basically consumes an item in an asynchronous way.

Do you see issues with this code?

public bool Push(T message)
{
    bool canEnqueue = true;

    lock (_vector)
    {
        _vector.Add(message);
        if (_vector.Count >= _maxSize)
        {
            canEnqueue = false;
        }
    }

    var onSignal = SignalEvent;
    if (onSignal != null)
    {
        onSignal();
    }

    return canEnqueue;
}
  • 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-13T14:19:56+00:00Added an answer on May 13, 2026 at 2:19 pm

    I know you said single-producer, multiple-consumer, but it’s worth mentioning anyway: if your queue is almost full (say 24 out of 25 slots), then if two threads Push at the same time, you will end up exceeding the limit. If there’s even a chance you might have multiple producers at some point in the future, you should consider making Push a blocking call, and have it wait for an “available” AutoResetEvent which is signaled after either an item is dequeued or after an item is enqueued while there are still slots available.

    The only other potential issue I see is the SignalEvent. You don’t show us the implementation of that. If it’s declared as public event SignalEventDelegate SignalEvent, then you will be OK because the compiler automatically adds a SynchronizedAttribute. However, if SignalEvent uses a backing delegate with add/remove syntax, then you will need to provide your own locking for the event itself, otherwise it will be possible for a consumer to detach from the event just a little too late and still receive a couple of signals afterward.

    Edit: Actually, that is possible regardless; more importantly, if you’ve used a property-style add/remove delegate without the appropriate locking, it is actually possible for the delegate to be in an invalid state when you try to execute it. Even with a synchronized event, consumers need to be prepared to receive (and discard) notifications after they’ve unsubscribed.

    Other than that I see no issues – although that doesn’t mean that there aren’t any, it just means I haven’t noticed any.

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

Sidebar

Related Questions

I would like to implement a producer/consumer scenario that obeys interfaces that are roughly:
I've spent today looking into lockless queues. I have a multiple producer, multiple consumer
I need to implement a 4-to-1 function in Veriog. The input is 4 bits,
I am using an sqlite database for a producer-consumer queue. One or more producers
I want to implement a Producer - Consumers pattern using a ThreadPool for the
I am using TPL support to implement a multithreading producer-consumer pattern in C#. With
I need to implement a B-Tree for University: I have an outer class B-Tree
I need to implement a Diff algorithm in VB.NET to find the changes between
I need to implement version control, even for just the developing I do at
I need to implement auto-capitalization inside of a Telerik RadEditor control on an ASPX

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.