Search code examples
c++c++11atomic

C++ atomic increment memory ordering


In C++11: If I increment an atomic variable (operator ++ on std::atomic), is the new value stored with a memory barrier? Or do I have to explicitly do a store?


Solution

  • You don't need to do an explicit store. The sequential consistency memory ordering will be used.

    operator++(int) and operator++() on atomic<integral> types are specified to have the effect of fetch_add(1), which ends up calling the member function with the default memory ordering memory_order_seq_cst.

    For the spec look around Requirements for operations on atomic types [atomics.types.operations.req] 29.6.5/33