C++ Logo

std-proposals

Advanced search

Re: [std-proposals] unique_lock<atomic_flag>

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Wed, 22 Feb 2023 18:52:44 -0500
On Wed, Feb 22, 2023 at 6:13 PM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> On Wed, Feb 22, 2023 at 9:29 PM Thiago Macieira via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >
> > > I don't see why a binary_semaphore would better than an atomic_flag here.
> >
> > That's because you haven't studied the code that this generates. It's worse
> > than a simple mutex or binary_semaphore.
>
>
> You can see the x86 assembler here on GodBolt:
>
> https://godbolt.org/z/aMva5qns7
>
> Could you please show me how you'd use a binary_semaphore to do the
> same job? I have an idea already of what you'd do but I just want to
> see how you'd do it.

You replace your `atomic_flag` with a `binary_semaphore`, and replace
your `while` loop with `acquire()` in the constructor, and have the
destructor do a `release()`.

Received on 2023-02-22 23:53:38