Multithreading – why is the compareandswap instruction considered expensive?
Why is the compareandswap instruction considered expensive?
I read a Book:
"Memory impairment is expensive, like the expensive atomic compareandset() instruction."
thank you!
Solution
"CAS" is no different from ordinary stores. Some error messages about CAS may come from the lock originally executed on Intel processor: cmpxchg (CAS). Lock: prefix causes the lock # signal to be asserted and access the bus. Of course, there is no scale Subsequent locking implementation: cmpxchg utilizes cache consistency protocol – usually based on snooping MESI rather than asserting lock #. – David Dice,Biased locking in HotSpot
This is true, for example On X86, the correct CAS on a multiprocessor system has a lock prefix The lock prefix causes a complete memory barrier:
In fact, the memory barrier is implemented as virtual lock or lock, and the on X86 / x64 Net and the Java JIT On X86, CAS causes a complete memory barrier
On PPC, it is different Ll / SC pair – lwarx & stwcx – can be used to load a memory operand into a register, write it back if there is no other storage at the destination, or retry the entire loop if it exists Ll / SC can be interrupted Nor does it mean an automatic full barrier Performance characteristics and behavior may vary greatly from architecture to architecture But again – weak ll / SC is not CAS