s ravi chandran wrote:
Does this represent the actual working of CAS, or is there anything essential that I am missing?
Well... you did implement a method that does "compare and swap", so, I guess you implemented CAS.... but...
Anyway, in general, when discussing CAS operations, it is based on the CAS instruction provided by the processor. The "S" in CAS could either mean "set" or "swap", but no matter, as either can be used to build upon. This instruction is atomic, across all processors/cores, and deals with the processor caches too. With
Java, access to the underlying processor's CAS instruction is provided by the Atomic variable support, in the java.util.concurrent.atomic package. And provided as compare and set.
So... Yes, you did implement "compare and swap", but what is the context here? Most engineering discussions around CAS are related to the processor / atomic / optimistic programming nature of it -- and not just a method that does a compare and swap.
Henry