[SOLVED] What is the difference between the ‘Compare And Swap’ and ‘Compare And Set’ operations?


This Content is from Stack Overflow. Question asked by taher bouali

Trying to understand the compare and swap operation briefly called CAS i found that it has a variant called compare and set. They work the same way but the return is different. compare and swap return a value but compare and set return a boolean. My question is did they use the same compare and exchange (CMPXCHG for x86) instruction on low-level. Are they both implemented by atomic classes in Java ?


Yes, it’s a wrapper that exposes the functionality of x86 lock cmpxchg or equivalent compare-and-swap instructions / sequences on other ISAs. But Java’s old compareAndSet API only returns the boolean success, not the old value on failure, so there are things it can’t quite do.

A recent question, Java 8 impl on Compare And Exchange (Not Compare and Set!) was about emulating Java17 compareAndExchange (returning the old value) in terms of Java compareAndSet – it can’t be done precisely. You can load a value from near the time of the exchange, but it might end up the same as the "expected" arg to your CAS even if your CAS failed. But if you keep both the boolean and the value you read separately, that’s fine for most algorithms.

This situation is like GNU C’s old __sync_bool_compare_and_swap vs. __sync_val_... for cases where you did want the old value. The latter is more powerful: you can get success/fail from the val version by comparing the return value yourself. So it’s a bit disappointing that Java would provide only the less powerful one from Java 8 until Java 17, although it is the one that a lot of algorithms would be more likely to use.

(GNU C later added __atomic builtins that match the C++11 API of compare_exchange_weak with a bool return value and the first arg by reference, updated on CAS failure. So you get either or both, depending on which outputs you bother to actually use.)

Also related:

This Question was asked in StackOverflow by taher bouali and Answered by Peter Cordes It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?