C++ Logo

std-proposals

Advanced search

[std-proposals] Freestanding try_lock_for and try_lock_until

From: Ted Lyngmo <ted_at_[hidden]>
Date: Mon, 18 Aug 2025 23:44:24 +0200
Hi!

I wanted to create a `unique_multilock` class template with all the
member functions of `unique_lock` but that is capable of locking
multiple lockables like `scoped_lock`.

Most of it seems pretty straight forward, but for _TimedLockable_s I
realized that `std::try_lock_for`/`std::try_lock_until` are "missing".

What I have in mind are two functions templates like this:
```
template <class Clock, class Duration, TimedLockable... Ls>
[[nodiscard]] bool
try_lock_until(const std::chrono::time_point<Clock, Duration>& tp,
Ls&... ls);

template <class Rep, class Period, TimedLockable... Ls>
[[nodiscard]] bool
try_lock_for(const std::chrono::duration<Rep, Period>& dur, Ls&... ls);
```
I wonder, are there any inherent reasons why we shouldn't have such
function templates in the standard library? If so, I won't pursure
writing a proposal. If it's just a case of "noone wanted them bad enough
to write a proposal", I may go ahead.

Best regards,
Ted Lyngmo

Ps. I have something of a reference implementation up for code review if
you'd like to take a peek at it:
> https://codereview.stackexchange.com/questions/297918/freestanding-try-lock-for-try-lock-until-pt-2

Received on 2025-08-18 21:44:29