std::atomic_flag Extensions

Get a brief background on 'std::atomic_flag' extensions.

We'll cover the following

Before I write about std::atomic_flag extension in C++20, I want to give a short reminder of std::atomic_flag in C++11. If you want to read more details, read my post about std::atomic_flag in C++11.

C++11

std::atomic_flag is a kind of atomic boolean. It has a clear- and set-state functions. I call the clear state false and the set state true for simplicity. Its clear member function enables you to set its value to false. With the test_and_set method, you can set the value to true and return the previous value. ATOMIC_FLAG_INIT enables initializing the std::atomic_flag to false.

std::atomic_flag has two exciting properties, it is

  • the only lock-free atomic.
  • the building block for higher thread abstractions.

With C++11, there is no member function to ask for the current value of a std::atomic_flag without changing it. This changes with C++20.

C++20 extensions

The following table shows the more powerful interface of std::atomic_flag in C++20.

Get hands-on with 1200+ tech skills courses.