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 7596231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:52:13+00:00 2026-05-30T21:52:13+00:00

I’m implementing a concurrent_blocking_queue with minimal functions: //a thin wrapper over std::queue template<typename T>

  • 0

I’m implementing a concurrent_blocking_queue with minimal functions:

//a thin wrapper over std::queue
template<typename T>
class concurrent_blocking_queue
{
    std::queue<T> m_internal_queue;
     //...
 public:
     void add(T const & item);
     T&   remove();
     bool empty();
};

I intend to use this for producer-consumer problem (I guess, it is where one uses such data structures?). But I’m stuck on one problem which is:

How to elegantly notify consumer when producer is done? How would the producer notify the queue when it is done? By calling a specifiic member function, say done()? Is throwing exception from the queue (i.e from remove function) a good idea?

I came across many examples, but all has infinite loop as if the producer will produce items forever. None discussed the issue of stopping condition, not even the wiki article.

  • 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-30T21:52:14+00:00Added an answer on May 30, 2026 at 9:52 pm

    My queues have usually used pointers (with an std::auto_ptr in the
    interface, to clearly indicate that the sender may no longer access the
    pointer); for the most part, the queued objects were polymorphic, so
    dynamic allocation and reference semantics were required anyway.
    Otherwise, it shouldn’t be too difficult to add an “end of
    file” flag to the queue. You’d need a special function on the
    producer side (close?) to set it (using exactly the same locking
    primitives as when you write to the queue), and the loop in the removal
    function must wait for either something to be there, or the queue to be
    closed. Of course, you’ll need to return a Fallible value, so that
    the reader can know whether the read succeeded or not. Also, don’t
    forget that in this case, you need a notify_all to ensure that all
    processes waiting on the condition are awoken.

    BTW: I don’t quite see how your interface is implementable. What does
    the T& returned by remove refer to. Basically, remove has to be
    something like:

    Fallible<T>
    MessageQueue<T>::receive()
    {
        ScopedLock l( myMutex );
        while ( myQueue.empty() && ! myIsDone )
            myCondition.wait( myMutex );
        Fallible<T> results;
        if ( !myQueue.empty() ) {
            results.validate( myQueue.top() );
            myQueue.pop();
        }
        return results;
    }
    

    Even without the myIsDone condition, you have to read the value into a
    local variable before removing it from the queue, and you can’t return a
    reference to a local variable.

    For the rest:

    void
    MessageQueue<T>::send( T const& newValue )
    {
        ScopedLock l( myMutex );
        myQueue.push( newValue );
        myCondition.notify_all();
    }
    
    void
    MessageQueue<T>::close()
    {
        ScopedLock l( myMutex );
        myIsDone = true;
        myCondition.notify_all();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I am doing a simple coin flipping experiment for class that involves flipping a
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.