Search code examples
c++visual-c++atomic

Set a value to POD within 1 atomic operation in VC++


I am trying to find a way to set a POD (int, byte, long etc.) as atomic operation. Notice std::atomic is not good for me because it is not POD.

I also looked in VC intrensic functions, but didn't find exactly what I wanted (guess due to lack of documentation).

Does anyone know how to do it?

EDIT: I did not know that std::atomic is POD, therefore my question was wrong, I'll reask it in a new question. I don't want to delete or alter this question because the answer is really good an informative. Thanks!!! My actual question: Set a value to POD (that doesn't have constructor/desctructor) within 1 atomic operation in VC++


Solution

  • Use std::atomic<T>, contrary to what your question states it is a POD type. Let's go through the POD requirements one by one:

    9 Classes [class]

    6 A trivially copyable class is a class that:

    • has no non-trivial copy constructors (12.8),
    • has no non-trivial move constructors (12.8),
    • has no non-trivial copy assignment operators (13.5.3, 12.8),
    • has no non-trivial move assignment operators (13.5.3, 12.8), and

    std::atomic<T> has no non-trivial copy or move constructors or assignment operators: it simply doesn't have them at all (they're deleted). It does have non-trivial non-copy non-move constructors and assignment operators, but that doesn't prevent the class from being trivially copyable.

    • has a trivial destructor (12.4).

    std::atomic<T> has a trivial destructor, see 29.5.

    A trivial class is a class that has a trivial default constructor (12.1) and is trivially copyable.

    std::atomic<T> has a trivial default constructor, see 29.5.

    The requirements add up: std::atomic<T> is a trivial class.

    10 A POD struct is a non-union class that is both a trivial class and a standard-layout class,

    std::atomic<T> is a non-union class, is a trivial class, and is a standard-layout class, see 29.5.

    and has no non-static data members of type non-POD struct, non-POD union (or array of such types).

    If std::atomic<T> had any members of type non-POD struct or union, one of those members would have to be a non-trivial class or a non-standard-layout class. That would prevent std::atomic<T> from being a trivial class or a standard-layout class*, and since we have already determined that it is a trivial class and a standard-layout class, it cannot have such a member.

    In the above, I've referred to 29.5. The relevant bit is p5:

    29.5 Atomic types [atomics.types.generic]

    5 The atomic integral specializations and the specialization atomic<bool> shall have standard layout. They shall each have a trivial default constructor and a trivial destructor. They shall each support aggregate initialization syntax.

    * There is one exception: it is hypothetically possible for a trivial class to contain a non-trivial class member. For this to apply to std::atomic<T>, it pretty much requires malice on the part of the implementer, so outside of a hypothetical DeathStation 9000, I'm discounting that possibility.