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..?
Regarding your second question: The mutex is locked in the entire
checkStatusfunction except during the execution oftimed_wait. And that is the key.I’m not sure what you intention with
myCallbackis. But to check the status I would add a status member tomyClass, set it ingetStatusand check it in theelsebranch of thetimed_wait. An example: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.