Daily bit(e) of C++ | std::timed_mutex, std::recursive_timed_mutex, std::shared_timed_mutex
Daily bit(e) of C++ ♻️19, Timed mutex variants that support deadline and timeouts for the try_lock method.
When a thread attempts to acquire a lock on a mutex, it will block until it is acquired, potentially indefinitely.
However, when working with operations that have a timeout/deadline, there is no point in waiting for a lock past the deadline.
The standard offers timed mutex variants (std::timed_mutex, std::recursive_timed_mutex and std::shared_timed_mutex) that support timeouts for the try_lock method:
for a duration: try_lock_for()
for a time point: try_lock_until()
This support is also exposed through std::unique_lock and std::shared_lock.