开发者

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

开发者 https://www.devze.com 2022-12-29 08:47 出处:网络
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.

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 lock开发者_StackOverflow中文版ing.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号