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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:16:31+00:00 2026-06-07T15:16:31+00:00

boost::condition_variable cond; boost::mutex mut; bool ready = false; void consumer() { boost::mutex::scoped_lock lock(mut); while

  • 0
boost::condition_variable cond;
boost::mutex mut;
bool ready = false;

void consumer() {
    boost::mutex::scoped_lock lock(mut);
    while (!ready) {
         cond.wait(lock);
    }
}

void producer() {
    boost::mutex::scoped_lock lock(mut);
    ready = true;
    cond.notify_all();
    boost::this_thread::sleep(boost::posix_time::seconds(4));
}

Refer to the above code, I actually sleep the producer thread for 4 seconds after I call notify_all(). However, the consumer threads are actually woken up after 4 seconds. So how can I get around this and wake up the consumer threads immediately after I call notify_all() despite the 4 seconds sleep. Thanks in advance.

  • 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-07T15:16:33+00:00Added an answer on June 7, 2026 at 3:16 pm

    It has to do with the boost::mutex::scoped_lock lock(mut); in producer.
    As the scope ends AFTER the sleep, the mutex is only released after it.

    Try with that, if you want to keep your scoped_lock.

    void producer() {
      {
        boost::mutex::scoped_lock lock(mut);
        ready = true;
        cond.notify_all();
      }
      boost::this_thread::sleep(boost::posix_time::seconds(4));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

boost::condition_variable cond; boost::mutex mutex; //thread #1 for(;;) { D * d = nullptr; while(
boost::condition_variable cond; boost::mutex mut; //thread1 { read_socket() cond.notify_one(); } //thread2 { for(;;) { ...
This is my scenario boost::condition_variable _condition; boost::unique_lock<boost::mutex> lock(_mutex); boost::detail::atomic_count _count; ......... _condition.wait(&lock, boost::bind(std::less<int>(), boost::ref(_count),
Is it possible to wait on a boost::condition_variable without having to acquire a mutex
Consider the following C++ member function: size_t size() const { boost::lock_guard<boost::mutex> lock(m_mutex); return m_size;
is boost::thread object thread-safe? Should I lock calling of member methods of boost::thread (e.g.
I have the following: typedef std::function<void(const EventArgs&)> event_type; class Event : boost::noncopyable { private:
I came across a strange Boost (v1.38) mutex deadlock in a preexisting .NET (C#,
I am currently writing an application(using boost) that will have one producer grabbing frames
I want to wait 1.5 seconds in a boost thread. Using boost::xtime I can

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.