C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Standard library should have a simple 'gate' for multithreading

From: Lewis Baker <lewissbaker_at_[hidden]>
Date: Fri, 22 Jul 2022 23:48:32 +1000
You can just use an atomic<bool> for this.

std::atomic<bool> gate{false}; // initially closed

// wait until open
gate.wait(false);

// wait until closed
gate.wait(true);

// open the gate
gate.store(true);
gate.notify_all();

// close the gate
gate.store(false);
gate.notify_all();

On Fri, Jul 22, 2022 at 10:44 PM, Ville Voutilainen via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Fri, 22 Jul 2022 at 12:49, Frederick Virchanza Gotham via
> Std-Proposals <std-proposals_at_[hidden]> wrote:
> > > > (1) you can open it
> > > > (2) you can close it
> > > > (3) you can wait for it to open
> > > > (4) you can wait for it to close
> > >
> > > You're describing a pair of std::binary_semaphore.
> > > https://en.cppreference.com/w/cpp/thread/counting_semaphore
> >
>
> > Messing around with two binary_semaphore's is nonsense, and if you're
> > working on a big project with a dozen other programmers, a bug will
> > creep in somewhere at some point (you'll get threadlock when somebody
> > does "pbs.first.acquire()" instead of "pbs.second.acquire()").
> >
> > The C++ Standard library should have a simple 'gate'.
>
> You mean like the barrier in
> https://www.open-std.org/JTC1/SC22/WG21/docs/papers/2015/n4538.pdf ?
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2022-07-22 13:48:44