Intel白皮书上的说明如下: (* Accumulator = AL, AX, EAX, or RAX depending on whether a byte, word, doubleword, or quadword comparison is being performed *) IF accumulator = DEST THEN ZF ← 1; DEST ← SRC; ELSE ZF ← 0; accumulator ← DEST; FI;
使用此原子操作可以实现自旋锁,之前有一篇文章中描述了实现:
Java代码
void lock(lock_t *lock) {
while (CompareAndExchange(&lock->flag, 0, 1) == 1)