2

Here is a snippet from Java library:

public final boolean compareAndExchangeAcquire(boolean expectedValue, boolean newValue) {
    return (int)VALUE.compareAndExchangeAcquire(this,
                                                (expectedValue ? 1 : 0),
                                                (newValue ? 1 : 0)) != 0;
}

It is from AtomicBoolean class. How can a cast to int return a boolean?

My main question: What is the difference between compareAndExchange vs compareAndExchangeAcquire?


In layman terms: statements written prior to xxxAcquire and after xxxRelease is free to reorder while applying xxx.

enter image description here

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • I am mainly looking for "what is the difference between `compareAndExchange` vs `compareAndExchangeAcquire`" – Concurrent Bhai Jul 13 '20 at 06:06
  • 3
    Yo should not change a question in such a drastic way. When your question is not about the syntactic construct of the posted code snippet, the entire code excerpt from `AtomicBoolean` becomes irrelevant, as you are now are asking a generic question about `VarHandle`. – Holger Jul 13 '20 at 09:03
  • @Holger Actually, I was little bit confused because I wanted to ask both of them. Sorry about that. I will keep this in mind. – Concurrent Bhai Jul 13 '20 at 09:07
  • @Holger I can't edit the question now since the answer is dependent on first question. What I really want is the answer to second one. Is this question too broad? – Concurrent Bhai Jul 13 '20 at 09:09
  • 3
    Perhaps, you want to open a new question instead. But make sure you’ve read Q&As like [this](https://stackoverflow.com/q/60119169/2711488) and [that](https://stackoverflow.com/q/56342141/2711488), as well as [this article](http://gee.cs.oswego.edu/dl/html/j9mm.html), so you can be as specific as possible regarding which aspect needs clarification. – Holger Jul 13 '20 at 10:08
  • @Holger Alright. Thanks, that article helps – Concurrent Bhai Jul 13 '20 at 10:09

1 Answers1

5

The last part of the code you posted is != 0. With clarifying variable:

int a = (int)VALUE.compareAndExchangeAcquire(this,
                                                (expectedValue ? 1 : 0),
                                                (newValue ? 1 : 0));
return a != 0;

Of course the != operator returns a boolean.

As for the second part of the question:

Also, what is the difference between compareAndExchange vs compareAndExchangeAcquire?

Firstly some required reading: https://stackoverflow.com/a/16181675/3424746

From the above answer you should understand that compilers/processors can reorder loads/stores, and the restrictions that acquires and releases place on those. Compare and exchange is most likely implemented with a CAS instruction, which can be viewed as a load+store. compareAndExchangeAcquire and compareAndExchangeRelease add the release/acquire semantics to the CAS/load+stores in question. In other words you can use these to prevent certain reorderings, or allow certain reorderings.

PiRocks
  • 1,708
  • 2
  • 18
  • 29
  • Yeah, I figured that out just after posting the question. How about "what is the difference between compareAndExchange vs compareAndExchangeAcquire" – Concurrent Bhai Jul 13 '20 at 05:58
  • Oh oops, sorry I didn't answer that last part, I'll edit my answer – PiRocks Jul 13 '20 at 06:00
  • 1
    Though I would say you should almost always use the non-acquire version, and not worry about it too much, its a semi-advanced concept. – PiRocks Jul 13 '20 at 06:01
  • I am curious about it, an example of acquiring the lock and releasing it would just work fine :) – Concurrent Bhai Jul 13 '20 at 06:02
  • You wrote: "In other words you can use these to prevent certain reorderings, or allow certain reorderings." Can you please explain which of the three versions prevents or allows "certain reorderings"? Further, shall we consider `compareAndExchange` to be the most conservative (safe)? – kevinarpe Aug 09 '20 at 15:19