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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:19:52+00:00 2026-06-01T03:19:52+00:00

I want to use boost::timed_wait to wait for an event or to timeout after

  • 0

I want to use boost::timed_wait to wait for an event or to timeout after 5 seconds. My problem is that my timed_wait only accepts the notification for the first time.

To be more precise:

I have some kind of little state machine. It does nothing more than just dispatch some Command asynchronously and then check if it was successfully. This means after dispatching the command my state machine calls m_Condition.timed_wait(lock,timeout). (m_Condition is a member variable with the type boost::condition_variable ).

Now if this asynchronous call was successfull it shall call a callback function which notifies m_Condition so I know everything went OK. When the command failed it doesn’t call the callback function and so my timed_wait should time out. So the callback function does nothing more and nothing less than calling m_Condition.notify_all().

The problem is that this only works the first time. This means, after the first time notify_all() has been called it doesn’t work with that condition variable again. I checked at my callback and it always calls notify_all() again but the timed_wait just times out.

Maybe some example code to make it a bit clearer:

myClass_A.hpp

class myClass_A
{
public:
    void runStateMachine();                 // Starts the state machine
    void callbackEvent();                   // Gets called when Async Command was successful
private:
    void stateMachine();                    // Contains the state machine
    void dispatchAsyncCommand();            // Dispatches an Asynchronous command
    boost::thread m_Thread;                 // Thread for the state machine
    boost::condition_variable m_Condition;  // Condition variable for timed_wait
    boost::mutex m_Mutex;                   // Mutex
};

myClass_A.cpp

void myClass_A::runStateMachine()
{
     m_Thread = boost::thread(boost::bind(&myClass_A,this));
}
void myClass_A::dispatchAsyncCommand()
{
     /* Dispatch some command Async and then return */
     /* The dispatched Command will call callbackEvent() when done */
}
void myClass_A::stateMachine()
{
    boost::mutex::scoped_lock lock(m_Mutex);
    while(true)
    {
        dispatchAsynCommand();
        if(!m_Condition.timed_wait(lock, boost::posix_time::milliseconds(5000)))
        {
            // Timeout
        }
        else
        {
            // Event
        }
    }
}
void myClass_A::callbackEvent()
{
    boost::mutex::scoped_lock lock(m_Mutex);
    m_Condition.notify_all();
}

So what can I do now? Is it not possible to use the condition_variable multiple times? Or do I need to reset it somehow? Any suggestions are welcome!

  • 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-01T03:19:53+00:00Added an answer on June 1, 2026 at 3:19 am

    Okay, not the brightest hour for me I actually solved the problem. The above posted code is compiling and works as intended. The actual problem lied within the handling of Event or Timeout. Since the stuff that happened there is absolutely unrelated to the timed_wait I guess I don’t need to explain it here. So this question can be closed.

    UPDATE:

    Of course the above code won’t work since the lack of a dispatch method. Just for the sake of it I am gonna mention it here: For testing purposes I just create an Object of myClass_A in my main function, start the state machine and then call the callbackEvent from my main function. This actually works!

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

Sidebar

Related Questions

I want to use boost::crc so that it works exactly like PHP's crc32() function.
I'd like to use boost::signals2 to handle event notification in my C++ app. I'm
I am using boost build in my project and now i want to use
I have a transaction log file in CSV format that I want use to
I want to use Boost C++ in a Windows 8 Metro app and I
I want to use a shared mutex so threads only get locked when a
I want to use boost::bind to create a boost::function inserting a new key-value pair
I wanted to use boost accumulators to calculate statistics of a variable that is
I want to use the boost map and the documentation says I need an
I want to use the recursive_directory_iterator offered by boost::filesystem to delete a directory. But

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.