I have a thread lets call it t1, that periodically sends something after x seconds. This "x seconds part" can be changed from other thread (t2). I am supposed to be able to do following from thread t1.
- wait for "x seconds" and then send something
- in between if the thread t2 sends another value of "x" , dont send but goto step 1.
I have used condition variable for this purpose with wait_for()
I want to only do a send when "x seconds" get over.
Currently I have implemented it without predicate (because I have no need for it) something like this:
auto done = wait_for(lock,x seconds);
if(done == cv_status::timeout)
{
/*perform send operation*/
}
but sometimes I see that the "sending happens" before timeout , which I suppose is due to spurious wakeup and missing predicate.
My question is how can I take care of spurious wakeup without having a predicate? should I follow another approach for this ? I dont have a need for predicate because thread t1 sleeps on a particular condition (when x is 0) and I want it(t1) to be woken up by t2 without any condition.
This is my first task working with cond variables and I am still learning CPP ,Thank you in advance.