C++ Logo


Advanced search

Re: [std-proposals] int Func(void) noreentry(-1)

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Thu, 2 Feb 2023 13:51:05 +0000
On Thu, Feb 2, 2023 at 12:17 PM Gergely Nagy <gergely.nagy.alt_at_[hidden]> wrote:
> This, especially noreentry_this_object is too convoluted to be a core language feature.

Please argue as to why.

> Such a keyword would have to be generic, covering both single- and multithreaded cases, and would require some state, that you can easily implement yourself (just like you did, with the mutex).

I didn't use a mutex. Look again.

> void no_reentry_fn() {
> static std::mutex prevent;
> if (!prevent.try_lock()) return;
> std::scoped_lock prevent_l(prevent);
> ...
> }

Your code will dead-lock if the same thread tries to re-enter. The
Standard says that "mutex::try_lock" has undefined behaviour if the
current thread has already locked it.

I used an atomic_flag, not a mutex.

Received on 2023-02-02 13:51:18