Can expression

variable = value; 

be interrupted "inside"? That is, is it possible during (after the beginning, but before the end) of writing to a variable another entry in the same variable?

    5 answers 5

    In general, the expression

     variable = value; 

    can be interrupted "inside". If variable is a variable of a complex type, for example, a struct, then for real assignment, several machine operations will be needed and after each operation a higher priority thread can be inserted. Atomicity can only be spoken for correctly aligned variables no larger than a machine word.

    • Exactly, I forgot about alignment. - Vladimir Gordeev
    • In fact, even if a variable of type int is not aligned, its transfer or assignment will still be an elementary operation and will be executed by a single processor command. Only the execution time of the command will be longer than with an operation with an aligned variable. And one processor command is always an atomic operation. - user576

    It is necessary to mark a variable as volatile . The variable must be located at the aligned address. (multiple to machine word)

    It is necessary that the variable be something that registers in the register: integer, pointer, character.

    In any case, it is better to always look at the output to verify this. ( gcc -S )

    • If the variable fits into a register, then volatile has nothing to do with it and you can safely do without it if it is NOT VOLUNT that is needed. - cy6erGn0m
    • volatile indicates to the compiler that the variable can be changed in a way that is not obvious to it and protects from optimizations, right? - Vladimir Gordeev
    • For sure. But this does not change the weather due to atomic / non-atomic nature. It affects a completely different. You can safely throw out volatile and not spoil the atomicity. This will only affect the timely availability of the variable to other threads. But in any case, they will receive either the old or the new version. - cy6erGn0m
    • Are you sure that after you throw out volatile, the compiler will not throw out your variable in order to optimize, or leave its new value to register for a while? - IronVbif
    • Maybe leave. But the question was not about;) - cy6erGn0m

    In C ++, for variables whose type is not std::atomic<T> , during the execution of the expression variable = value writing and reading this variable from other threads is impossible,
    because otherwise there would be a data race (data race), and there can be no races in the correct C ++ program (otherwise there will be undefined behavior, UB).

    For variables with the type std::atomic<T> entry is atomic by definition of atomicity in C ++.

      I think if value is a constant, and variable is declared as volatile , then another thread will not interpose in the middle of this operation.

      • 3
        You can easily hook in when assigning something that does not fit into the register. For example, a 64bit integer on a 32bit processor. - cy6erGn0m

      If access to a variable occurs as a whole (ie, a record, for example, a dvord or fpu state), then the operation is atomic. If the variable is complex, then synchronization is required. Ie in the simplest case, the variable is dvd, and you refer to it as a byte. Hardwarno decides to use the prefix Lock. It makes no sense to programmatically discuss, since this is a problem script.