views:

176

answers:

1

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?

+2  A: 

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.

Potatoswatter
@Potato I see that you have followed the advice I give to Steve. You should let Steve to do it himself.
Vicente Botet Escriba
@Vic I didn't copy the idea from the comments, if that's what you mean. I can make this CW if you'd feel better.
Potatoswatter
@Potatowatter Well I accept that you can have the same idea. I agree that the interface provided by the standard is minimal and allows the user to do whatever she wants. It is normal you don't find lock_until because it is part of my question. The question is do you prefer the code you written or a specific function that throw a common exception.
Vicente Botet Escriba
@Vicente: Personally, I would define the function to generate the exception as soon as I defined the new exception class, assuming there were several such deadlines to miss. I dislike copy-paste code. But overspecified functions that only get called in one place are also bad.
Potatoswatter
+1 I have finally accepted your answer as, as the user can always implement it easily. but I really think however that the addition of exception based timed locks open a window for mush more friendly features.
Vicente Botet Escriba