C++ Logo


Advanced search

Re: [std-proposals] unique_lock<atomic_flag>

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Wed, 22 Feb 2023 00:11:38 +0000
On Wed, 22 Feb 2023, 00:02 Frederick Virchanza Gotham via Std-Proposals, <
std-proposals_at_[hidden]> wrote:

> I think 'lock_guard' and 'unique_lock' should be usable with atomic_flag.
> The constructor would do:
> while ( f.test_and_set() ) f.wait(true);
> And the destructor would do:
> f.clear();
> Furthermore, 'try_lock' would simply be:
> return false == f.test_and_set();
> Also defer_lock, adopt_lock and try_to_lock would be easily
> implementable as constructor arguments.

It would certainly be possible to do that, but would it be useful?

Why would you use an atomic_flag this way?

If you want something that behaves like a mutex, you can just use a mutex.
Or you can create a type that meets the Lockable requirements by using an
atomic_flag for the lock/unlock/try_lock members as described above, then
you can use it with any of lock_guard, unique_lock, or scoped_lock.

Received on 2023-02-22 00:11:53