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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:30:47+00:00 2026-06-13T00:30:47+00:00

I have an engine that asynchronously process tasks and for one task I want

  • 0

I have an “engine” that asynchronously process tasks and for one task I want to wait until that task is processed.

boost::condition_variable cvWorkDone;

DoSomeWork()
{
   PostAsyncJob(DoWorkAsync)   // is a boost::asio::post

   boost::mutex::scoped_lock lock(mtxWorkDoneCv);
   cvWorkDone.wait(lock);
}


DoWorkAsync()
{
   // do some work ...

   cvWorkDone.notify_one();
}

The problem is that the code above has a race condition. What if DoWorkAsync() notifies the boost::condition_variable before DoSomeWork() waits for it ?

I see that boost::condition_variable::wait has a second parameter, a bool that can be used to implement something like this

bool bWait;

DoSomeWork()
{
   bWait = true;
   PostAsyncJob(DoWorkAsync)   // boost::asio::post

   boost::mutex::scoped_lock lock(mtxWorkDoneCv);
   cvWorkDone.wait(lock, bWait);
}


DoWorkAsync()
{
   // do some work ...
   boost::mutex::scoped_lock lock(mtxWorkDoneCv);
   cvWorkDone.notify_one();      
   bWait = false;
}

but the concurrency is still there … How can I solve this ?

  • 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-13T00:30:49+00:00Added an answer on June 13, 2026 at 12:30 am

    Since condition variables don’t maintain state about whether they’ve been signaled or not, you need to maintain the state for whatever might be the reason for the condition variable to be signaled separately (in some cases, such as queues, the reason for a condition variable to be signaled can go away asynchronously). So you might have something like this in your code:

    boost::condition_variable cvWorkDone;
    bool workdone = false;
    
    DoSomeWork()
    {
       PostAsyncJob(DoWorkAsync)   // is a boost::asio::post
    
       boost::mutex::scoped_lock lock(mtxWorkDoneCv);
       while (!workdone) {
          cvWorkDone.wait(lock);
       }
    }
    
    
    DoWorkAsync()
    {
       // do some work ...
    
       {   
          boost::mutex::scoped_lock lock(mtxWorkDoneCv);
          workdone = true;
       }
       cvWorkDone.notify_one();
    }
    

    Note that this also protects against spurious returns from boost::condition_variable::wait(). From the boost docs on boost::condition_variable::wait():

    The thread will unblock when notified by a call to this->notify_one() or this->notify_all(), or spuriously.

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

Sidebar

Related Questions

I have a Rails engine that defines a class method top(count) on a model
I have a game engine that uses OpenGL for display. I coded a small
I have a search engine that searches albums. For each music album, I have
I have an XPath engine implementation that needs to be tested. Is there a
So i have been building an AR engine that makes use of the magnetic
So, i have a problem with my engine that i am working on right
I have this chess engine Rybka.exe, that i have to execute in java Here
I'm considering using JRuby on App Engine but have heard that Juby app on
I have a custom HttpModule rewrite engine in an existing web application project that
I have a TYPO3 site with Indexed Search Engine extension... The problem is that

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.