volatile reads does happen before volatile write in JMM?

101 views
Skip to first unread message

r r

unread,
Sep 14, 2022, 6:51:15 AM9/14/22
to mechanical-sympathy
Hello,
let's look for the following piece of code:

int x;
volatile boolean v; // v = false by default
T1:  
    x = 1;       (1)
    v = true;    (2)
    doSth        (3) 

T2:  
   doSth2        (4)   
   if (v) {}     (5)
      
   
When T2 observes that v == false in (5), does it mean that there is a happens-before relation (4) -> (5) -> (2) -> (3)?

What if v would be AtomicBolean?

Peter Veentjer

unread,
Sep 14, 2022, 7:26:06 AM9/14/22
to mechanica...@googlegroups.com
No.

There can't be a happens-before edge between a read of v (5) and a write of v (4).

The volatile write/read will be ordered in the synchronization order, but not in the synchronized-with order and therefore not ordered by happens-before  (since the happens-before order is the transitive closure of the union of the synchronizes-with order and the program order).

Only when a volatile read sees a particular volatile write, then there is a happens-before edge from the write to the read, but never in the opposite direction.
 

What if v would be AtomicBolean?

Doesn't change anything since an AtomicBoolean get/set has the same semantics as a volatile read/write.

 

--
You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/mechanical-sympathy/faf3e288-f249-4d07-830b-7752bb41a472n%40googlegroups.com.

r r

unread,
Sep 14, 2022, 8:04:16 AM9/14/22
to mechanica...@googlegroups.com

r r

unread,
Sep 15, 2022, 4:43:39 AM9/15/22
to mechanical-sympathy
What about a such case:

AtomicLong x;
volatile boolean v;

T1:
   v = true;                                     (1)
   long a = x.incrementAndGet(); // a == 1       (2)
T2:
   long b = x.incrementAndGet(); // b == 2       (3)

Do I understand correctly that if T2 observes that x == 2 it also means that T2 observer v == true because of happens-before (1) -hb-> (2) -hb-> (3)

Peter Veentjer

unread,
Sep 15, 2022, 6:40:57 AM9/15/22
to mechanica...@googlegroups.com
On Thu, Sep 15, 2022 at 11:43 AM r r <gros...@gmail.com> wrote:
What about a such case:

AtomicLong x;
volatile boolean v;

T1:
   v = true;                                     (1)
   long a = x.incrementAndGet(); // a == 1       (2)
T2:
   long b = x.incrementAndGet(); // b == 2       (3)

Do I understand correctly that if T2 observes that x == 2 it also means that T2 observer v == true because of happens-before (1) -hb-> (2) -hb-> (3)

'yes'.

So imagine 'v' would be plain and you would have the following code:

T1:
v=true                                       (1)
x.incrementAndGet();                 (2)

T2:
if(x.incrementAndGet()==2){          (3)
   print(v)                                                (4)
}

If (3) observes (2), then there is a happens-before edge between (1) and (4) because:

(1) happens-before (2) due to program order rule
(2) happens-before (3) due to volatile variable rule
(3) happens-before (4) due to program order rule

Since happens-before is transitive, there is a happens-before edge between (1) and (4).

So since we have a happens-before edge when v is plain, we certainly have a happens-before edge is v would be volatile.
 

r r

unread,
Sep 15, 2022, 11:17:32 AM9/15/22
to mechanica...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages