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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:17:08+00:00 2026-06-01T00:17:08+00:00

I am trying to use the timed_wait from boost. Now I am actually not

  • 0

I am trying to use the timed_wait from boost. Now I am actually not quite sure how to do this.

The purpose of the whole thing is to determine it’s state. In the code below a function getStatus() is called. This function is async and if everything went right it calls a specific callback function to indicate that everything is allright. If it not calls the callback in time ( so timeout occurs ) I know something went wrong.

So here is the example code:

void myClass::checkStatus()
{
    boost::mutex::scoped_lock lock(m_Mutex);
    boost::condition_variable cond;
    while(true)
    {
        getStatus();  // Async Call to get the actual status
        if(!cond.timed_wait(lock,boost::posix_time::milliseconds(3000),/* Callback */))
        { 
           // Timeout
        }
        else
        {
            // OK 
        }
    }
}
bool myClass::myCallback()
{
/* ... */
}

So, as you can see, I don’t know how to appropriately “add” the callback to my timed_wait. Actually I don’t actually get it how it will work since I expect my Callback to be called from my async thread and not from the timed_wait itself? ( The async thread needs to signalize that everything went well )

I also looked into the Boost Documentation but it couldn’t help me much more.

On a second question: Is my mutex in this example always locked..?

  • 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-01T00:17:10+00:00Added an answer on June 1, 2026 at 12:17 am

    Regarding your second question: The mutex is locked in the entire checkStatus function except during the execution of timed_wait. And that is the key.

    I’m not sure what you intention with myCallback is. But to check the status I would add a status member to myClass, set it in getStatus and check it in the else branch of the timed_wait. An example:

    boost::condition_variable m_cond;
    
    void myClass::checkStatus()
    {
        boost::mutex::scoped_lock lock(m_Mutex);
    
        while(true)
        {
            getStatus();  // Async Call to get the actual status
            if(!m_cond.timed_wait(lock,boost::posix_time::milliseconds(3000),/* Callback */))
            { 
               // Timeout
            }
            else
            {
                //check status
                if (m_status == STATUS_1)
                {
                    // handle STATUS_1
                }
                else if (m_status == STATUS_2)
                {
                    // handle STATUS_2
                }
             }
        }
    }
    
    void getStatus()
    {
        boost::thread myThread(myWorkerFunc);  
    }
    
    void myWorkerFunc()
    {
        // do a long running operation to get the status
        int status = retrieveStatusSomehow();
    
        // lock the same mutex that you used for the condition variable
        boost::mutex::scoped_lock lock(m_Mutex);
    
        // assign the status to member
        m_status = status;
    
        // notify the condition to wake up
        m_cond.notify_all();
    }
    

    I hope it is clearer now. Maybe you can integrate your callback function in this approach. Also you should consider to cancel the background thread in case of a timeout to avoid race conditions.

    Edit: Note that the condition variable must be a member to notify it about the end of the async operation.

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

Sidebar

Related Questions

I'm trying to use xmlrpc client from many QThreads. To make sure that only
I am trying to use Swing Timer and I wanted to start from a
this is my first time trying to use document classes in AS3 and im
I am trying to use prepared statements which operate on a database located quite
I am trying to build some big libraries, like Boost and OpenCV , from
I'm trying to use WebClient to download a bunch of files asynchronously. From my
I'm trying to use the Win32 API to make a sub-thread that reads from
I am trying to use Boost::asio and async_read to implement a timeout in protocol
I am trying to learn how to use the C++ Boost.Thread library. I have
Am trying to re-use a function i got from one of the threads on

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.