The i += 10;
statement maintains the atomicity of i
, because it is used as an lvalue expression. From cppreference (bolding mine):
Built-in increment and decrement operators and compoundassignment are read-modify-write atomic operations with totalsequentially consistent ordering (as if using memory_order_seq_cst).If less strict synchronization semantics are desired, the standardlibrary functions may be used instead.
The example given on the linked page uses the built-in (pre-)increment operation on the acnt
variable, but it could just as well have used a compound assignment, as your code does.
However, more complex arithmetic operations may cause the i
variable to lose its atomicity, if it is not used strictly as an lvalue expression. From the same page:
Atomic properties are only meaningful for lvalue expressions.Lvalue-to-rvalue conversion (which models a memory read from an atomiclocation to a CPU register) strips atomicity along with otherqualifiers.