I have a condition variable std::condition_variable my_cond;
I want to be able to replace boost::timed_wait() with an std equivalent one.
If the previously mentioned condition variable was a boost one, then I would use the boost version of timed_wait() like this:
cond_.timed_wait(lock,timeout);
where lock is std::scoped_lock lock(mutex_);
and timeout is a time that is performed with boost::chrono.
boost:chrono, boost locks and boost mutexes have been replaced with their std analogues. I have yet to find an alternative to timed_wait().
I try to find a function belonging to std, where it receives the lock as the first parameter and an std::chrono type time as a second parameter.
Looking at the documentation of the condition_variable::wait here:
https://en.cppreference.com/w/cpp/thread/condition_variable/wait
I don't see an overloaded wait function where the second argument is a time type.
Is this conversion from boost::timed_wait() to std::wait() possible?
EDIT: After trying users' Tony Salimi and Howard Hinnant suggestions, unofrtunately i get an error message. This is the error message:
error: no matching function for call to
std::condition_variable::wait_for(std::scoped_lock<std::mutex>&, const std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long int, std::ratio<1, 1000000000> > >&)’
This is the line that the error is given:
if(!my_cond.wait_for(lock,timeout))
timeout is: auto const timeout = std::chrono::system_clock::now() + std::chrono::milliseconds( ms );
lock is: std::scoped_lock lock(mutex_); where mutex_ is mutable std::mutex mutex_;
wait_formethod, documented here?