Are +=, |=, &= etc atomic?

atomic, c, c++, operators, thread-safety

Solution

You are wrong. There is no guarantee that ++ is atomic and neither is there for the compound assignment operators, or indeed for any C++ operation.

Problem

Are the "modify" operators like `+=`, `|=`, `&=` etc atomic? I know `++` is atomic (if you perform `x++;` in two different threads "simultaneously", you will always end up with `x` increased by 2, as opposed to `x=x+1` with optimization switched off.) What I wonder is whether `variable |= constant`, and the likes are thread-safe or do I have to protect them with a mutex? (...or is it CPU-dependent? In this case, how is it on ARM?)

Original source

Related problems