Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Compare-and-swap
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Example application: atomic adder=== As an example use case of compare-and-swap, here is an algorithm for [[fetch-and-add|atomically incrementing or decrementing an integer]]. This is useful in a variety of applications that use counters. The function {{mono|add}} performs the action {{mono|*p β *p + a}}, atomically (again denoting pointer indirection by {{mono|*}}, as in C) and returns the final value stored in the counter. Unlike in the {{mono|cas}} pseudocode above, there is no requirement that any sequence of operations is atomic except for {{mono|cas}}. '''function''' add(p: pointer to int, a: int) returns int done β false '''while''' not done value β *p // Even this operation doesn't need to be atomic. done β cas(p, value, value + a) '''return''' value + a In this algorithm, if the value of {{mono|*p}} changes after (or while!) it is fetched and before the CAS does the store, CAS will notice and report this fact, causing the algorithm to retry.<ref>{{cite web |url=https://www.ibm.com/developerworks/library/j-jtp11234/index.html |title=Java theory and practice: Going atomic |website=IBM developerWorks |date=23 November 2004 |first=Brian |last=Goetz}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)