C++ Logo


Advanced search

Re: [std-proposals] Idea for a "null" mutex

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Mon, 02 Jan 2023 10:45:16 +0000
On 2 January 2023 09:46:21 GMT, Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]> wrote:
>On Monday, January 2, 2023, Simon Scatton via Std-Proposals <
>std-proposals_at_[hidden]> wrote:
>> So that for instance if I want a thread safe Output, I use this:
>> Output<std::mutex> A;
>> And if I want a non-thread safe Output I use this:
>> Output<NullMutex> B;
>What if you were to pass around an 'std::optional<std::mutex>'?
>Then the likes of 'lock_guard' and 'unique_lock' could be given a new
>overloaded constructor:
>template<class MutexType>
>class lock_guard {
> lock_guard(std::optional<MutexType> &arg)
> {
> if ( arg.has_value() ) p_mtx = &arg.value();
> }

You can probably do this with unique_lock, but not with lock_guard. A current implementation can assume that p_mtx is not null and not bother with a null check in the destructor. Changing the destructor definition would be an ABI break.

Anyway, I don't see how this would solve the original problem efficiently. As originally described, Output<NullMutex> would not hold a std::optional<std::mutex> member.


Received on 2023-01-02 10:45:22