Tutorial :Why timed lock doesn't throw a timeout exception in C++0x?


C++0x allows to lock on a mutex until a given time is reached, and return a boolean stating if the mutex has been locked or not.

template <class Clock, class Duration>  bool try_lock_until(const chrono::time_point<Clock,                       Duration>& abs_time);  

In some contexts, I consider an exceptional situation that the locking fails because of timeout. In this case an exception should be more appropriated.

To make the difference a function lock_until could be used to get a timeout exception when the time is reached before locking.

template <class Clock, class Duration>  void lock_until(const chrono::time_point<Clock,                   Duration>& abs_time);  

Do you think that lock_until should be more adequate in some contexts? if yes, on which ones? If no, why try_lock_until will always be a better choice?


Can't you just check the return value and throw your own exception?

if ( ! my_lock.try_lock_until( my_start_time ) ) {      throw realtime_error( "Couldn't start in time!" );  }  

Also, a quick look through the threading and exceptions libraries in the FCD doesn't show any time-related exception classes, so there's no type in std:: for lock_until to naturally throw.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »