I would like to know which is the difference between:
boost::timed_mutex _mutex;
if(_mutex.timed_lock(boost::get_system_time() + boost::posix_time::milliseconds(10))){
exclusive code
_mutex.unlock();
}
and
boost::timed_mutex _mutex;
boost::timed_mutex::scoped_lock scoped_lock(_mutex, boost::get_system_time() + boost::posix_time::milliseconds(10));
if(scoped_lock.owns_lock()) {
exclusive code
}
I do know already that the scoped_lock makes unnecessary the call to unlock. My question refers to:
- Why in the first case we call timed_lock as member function of a mutex and in the second one we construct a lock from a mutex and a duration.
- Which one is more efficient?
- The usage of boost::posix_time is okay or is recommended to use another kind, e.g. chrono or duration?
- There's a better way (faster) to try to acquire a lock for 'x' time than the two methods above specified?