Search code examples
c++c++11atomicstdatomic

Is ++ atomic for std::atomic<int>


Acording to one Channel 9 E2E video(with Herb Sutter in it) in c++0x if number is atomic<int> number++ is atomic. Can somebody confirm that is how it is in the final C++11 standard(lets pretend that it is finalized :)).


Solution

  • The standard is finalised, and every operation on all the standard integral specialisations of atomic<T> is atomic.

    This doesn't mean all expressions involving standard integral atomic<T> are atomic.

    number = number * 2;
    

    is two operations:

    temporary = number * 2;
    number = temporary;
    

    Each of them is atomic, but together they are not. This is what transactions/critical sections are for.