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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:55:53+00:00 2026-06-14T22:55:53+00:00

boost::condition cond; boost::mutex access; void listener_thread() { boost::mutex::scoped_lock lock(access); while (true) { while (!condition_check_var)

  • 0
boost::condition cond;
boost::mutex access;
void listener_thread()
{
  boost::mutex::scoped_lock lock(access);

  while (true) {
      while (!condition_check_var) {
          cond.wait(lock);
      }
      do_some_work();
  }
}

/// ... Main thread ...
cond.notify_all();
check_work:
{
    boost::mutex::scoped_lock lock(access);
    function_relies_on_work_been_done();
}

Is this proper design? Is it safe to assume that once the notify_all() returns, the listener_thread will have already acquired the lock? And that when the check_work block will run (since it’s locking the same mutex as the listener_thread()), some “work” will have already been done by the listener_thread()?

If not, what is the preferred way to achieve this kind of behavior?

  • 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-14T22:55:55+00:00Added an answer on June 14, 2026 at 10:55 pm

    There is no guarantee that any other thread has acted upon a notification or even, yet, received. In fact, there isn’t even a guarantee that there is a thread currently waiting for its reception although in your setup it looks as if it is likely the case that there are threads waiting. If you want to make sure that the receiving threads have done their work you’ll need to set up a reverse communication channel, e.g., using another condition variable and a suitable condition.

    I realize that your question is about Boost but here is what the standard has to say about this (30.5.1 [thread.condition.condvar] paragraph 8):

    void notify_all() noexcept;
    Effects: Unblocks all threads that are blocked waiting for *this.

    It doesn’t give any guarantee about what happens to the threads and/or any involved mutex.

    • 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 mut; bool ready = false; void consumer() { boost::mutex::scoped_lock lock(mut); while
boost::condition_variable cond; boost::mutex mutex; //thread #1 for(;;) { D * d = nullptr; while(
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),
Consider the following C++ member function: size_t size() const { boost::lock_guard<boost::mutex> lock(m_mutex); return m_size;
Is it possible to wait on a boost::condition_variable without having to acquire a mutex
boost::condition_variable cond; boost::mutex mut; //thread1 { read_socket() cond.notify_one(); } //thread2 { for(;;) { ...
I have a function of the form: void DoSomething(const boost::function<bool ()>& condition, other stuff);
is boost::thread object thread-safe? Should I lock calling of member methods of boost::thread (e.g.
on linux, gcc 4.3, compiling a class with boost::thread implementation and mutexes / condition
I'm looking for a way to wait for multiple condition variables. ie. something like:

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.