Compare-and-swap Compare-and-swap

Compare-and-swap - Definition and Overview

In computer science, the compare-and-swap CPU instruction is a special instruction that atomically compares the contents of a memory location to a given value and, if they are the same, modifies the contents of that memory location to a given new value. It is used to implement semaphores in multiprocessor systems.

It is also used to implement lock-free (non-blocking) algorithms in multiprocessor systems, something that Maurice Herlihy proved cannot be done with only read, write, and test-and-set.

In uniprocessor systems, it is sufficient to disable interrupts before accessing a semaphore.

However, in multiprocessor systems, it is impossible and undesirable to disable interrupts on all processors at the same time; and even with interrupts disabled, two or more processors could be attempting to access the same semaphore's memory at the same time. The compare-and-swap instruction allows any processor to atomically test and modify a memory location, preventing such multiple processor collisions.

See also

  • Fetch-and-add
  • Load-Link/Store-Conditional

External links

Example Usage of Compare-and-swap

mskwt: @k1ha410 うお ARM の例か。Intel x64, x86 だと CICS なので lock (prefix) {mov, ...} reg, mem できます。cmpxchng が compare and swap 2-words の鍵ですね
Copyright 2009 WordIQ.com - Privacy Policy  :: Terms of Use  :: Contact Us  :: About Us
This article is licensed under the GNU Free Documentation License. It uses material from the this Wikipedia article.