On Wed, 22 Feb 2023, 00:02 Frederick Virchanza Gotham via Std-Proposals, <std-proposals@lists.isocpp.org> 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:


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.