原子操作定义和多处理器

atomic operation definition and multiprocessor

我正在学习同步,现在我对原子操作的定义感到困惑。通过查找,只知道原子操作是不间断操作。

那么,原子操作不是只对单处理器系统有效吗,因为对于多处理器系统,许多操作可以同时运行?

This link 解释得非常完美(强调我的):

On multiprocessor systems, ensuring atomicity exists is a little harder. It is still possible to use a lock (e.g. a spinlock) the same as on single processor systems, but merely using a single instruction or disabling interrupts will not guarantee atomic access. You must also ensure that no other processor or core in the system attempts to access the data you are working with. The easiest way to achieve this is to ensure that the instructions you are using assert the 'LOCK' signal on the bus, which prevents any other processor in the system from accessing the memory at the same time. On x86 processors, some instructions automatically lock the bus (e.g. 'XCHG') while others require you to specify a 'LOCK' prefix to the instruction to achieve this (e.g. 'CMPXCHG', which you should write as 'LOCK CMPXCHG op1, op2').