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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:36:21+00:00 2026-06-11T19:36:21+00:00

I try to implement blocking queue. the main parts are the following (it’s a

  • 0

I try to implement blocking queue. the main parts are the following (it’s a kind of educational task)

template <typename T>
class Blocking_queue
{
public:
    std::queue<T> _queue;
    boost::mutex _mutex;
    boost::condition_variable _cvar;

    void Put(T& object);
    T Get();
    void Disable()
};


template<typename T>
void Blocking_queue::Put(T& object)
{
    boost::mutex::scoped_lock lock(_mutex);
    _queue.push(T);
    lock.unlock();
     _cvar.notify_one();
}

template<typename T>
T Blocking_queue::Get()
{
    boost::mutex::scoped_lock lock(_mutex);

    while(_queue.empty())
    {
        _cvar.wait(_mutex);
    }

    T last_el = _queue.front();
    _queue.pop();
    return last_el;
}

template<typename T>
void Blocking_queue::Disable()
{

}

And i need to implement a function Disable() “releasing” all waiting threads (as written in the task). The problem is that i don’t fully understand what “releasing” in this terms means, and what methods should i apply. So my idea – is the following: when Disable() is called we should call some method for current thread in this place (inside the loop)

    while(_queue.empty())
    {
        //here
        _cvar.wait(_mutex);
    }

which will release current thread, am i right? Thanks.

  • 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-06-11T19:36:22+00:00Added an answer on June 11, 2026 at 7:36 pm

    “releasing all threads that are waiting” is an operation that is hardly useful. What do you want to do with this operation?

    What is useful, is to shutdown the queue, thus every thread waiting on the queue will be unblocked and every thread that is going to call Get() will return immediately. To implement such a behaviour, simply add a shutdown flag to the queue and wait for “not empty or shutdown”:

    template<typename T>
    void Blocking_queue::Disable()
    {
        boost::mutex::scoped_lock lock(_mutex);
        _shutdown = true;
    
        _cvar.notify_all()
    }
    

    To indicate that there is no data, to the caller of Get(), you could return a pair with an additional bool or throw a special exception. There is no way to return null, as not for all types T there is a null value.

    template<typename T>
    std::pair< bool, T > Blocking_queue::Get()
    {
        boost::mutex::scoped_lock lock(_mutex);
    
        while  (_queue.empty() && !_shutdown )
            _cvar.wait(_mutex);
    
        if ( _shutdown )
            return std::make_pair( false, T() );
    
        T last_el = _queue.front();
        _queue.pop();
        return std::make_pair( true, last_el );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to implement C++ properties as templates as defined in WikiPedia template <typename
If I try to implement my class on this file I get an error
I try to implement the abstract DbCommand class (like OdbcCommand, OleDbCommand, ...) but a
I try to implement a TypedActor in Java following the examples on Typed Actors
I try to implement the following code, this page will display when survey is
I try to implement the following code: <!DOCTYPE html> <html lang=en> <head> <meta http-equiv=Content-Type
I try to implement a table function by following Tony Andrews example . But
I try to implement the animation: when you enter iPhone Gallery, press the image,
I try to implement footer datagrid with allowMultipleSelection properties. But in my case, it
I try to implement a browser-like app. I want to let it can open

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.