Hi,
In Qt, we've stumbled across the following issue, when trying to make QMutex
model TimedLockable:
We model it by providing inline methods try_lock{,_for,_until}() that delegate
to the legacy QMutex functions that take an int representing milliseconds.
So far so simple.
The question is what type try_lock_for() should take. Remember, QMutex
fundamentally only supports millisecond granularity, so we could either take
any duration and perform the duration_cast to milliseconds inside the
function, breaking a design rule of <chrono>, namely that narrowing duration
conversions should be explicit, or just take std::chrono::milliseconds,
forcing users to be explicit about the narrowing, using a duration_cast at the
call site.
By hiding the conversion inside the try_lock_for function, we make it
implicit, so misleading code such as
mutex.try_lock_for(10us);
compiles, but just does a try_lock(), without any waiting time.
If we take only milliseconss, does this still model TimedLockable?
More generally, if the underlying API (Qt, platform-native, machine-native,
...) cannot deal with a given (finer) resolution, should the API support it?
Should, say,
using femtoseconds = std::chrono::duration<int, std::femto>;
std::decltype<std::timed_mutex&>().try_lock_for((femtoseconds(10));
compile?
Thanks,
Marc
--
Marc Mutz <
marc...@kdab.com> | Senior Software Engineer
KDAB (Deutschland) GmbH & Co.KG, a KDAB Group Company
Tel:
+49-30-521325470
KDAB - The Qt, C++ and OpenGL Experts