C++ Logo

std-proposals

Advanced search

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

From: Scott Michaud <scott_at_[hidden]>
Date: Mon, 2 Jan 2023 01:46:26 -0500
Is this what you're asking for?

https://godbolt.org/z/3v9zY8Ke8

On Sun, Jan 1, 2023 at 10:22 PM Simon Scatton via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Hello everyone,
>
> I am currently implementing a generic logger that should be thread-safe.
> To make sure I rewrite as little code as possible, I am templating a class
> as
> follows:
>
> template <typename Mutex>
> class Output
> {
> public:
> void write();
> protected:
> Mutex mutex_;
> };
>
> And I have a "null mutex" that is a struct like this:
> struct NullMutex
> {
> void lock() {}
> void unlock() noexcept {}
> bool try_lock() { return true; }
> };
>
> 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;
>
> The core of the write() function being something like this:
>
> template <typename Mutex>
> void Output<Mutex>::write()
> {
> const std::lock_guard<Mutex> lock(mutex_);
> // ...
> }
>
> I have not found any C++ official resources about this.
>
> Has this been already addressed in C++ standard discussion around
> mutexes ? Do you think adding such a "null mutex" would be useful?
>
> I would love to hear what you think about this.
>
> Kind regards,
>
> --
> Simon Scatton
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2023-01-02 06:46:39