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)*? 

środa, 14 września 2022 o 14:04:16 UTC+2 r r napisał(a):

> Thanks 
>
> śr., 14 wrz 2022, 13:26 użytkownik Peter Veentjer <[email protected]> 
> napisał:
>
>>
>>
>> On Wed, Sep 14, 2022 at 1:51 PM r r <[email protected]> wrote:
>>
>>> 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)?
>>>
>>
>> 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 [email protected].
>>> To view this discussion on the web, visit 
>>> https://groups.google.com/d/msgid/mechanical-sympathy/faf3e288-f249-4d07-830b-7752bb41a472n%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/mechanical-sympathy/faf3e288-f249-4d07-830b-7752bb41a472n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> -- 
>> 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 [email protected].
>>
> To view this discussion on the web, visit 
>> https://groups.google.com/d/msgid/mechanical-sympathy/CAGuAWdCqwmySqhNkHRKZcouwKf0F3oM%3DHb_byqLwY8D3Hyb-Gg%40mail.gmail.com
>>  
>> <https://groups.google.com/d/msgid/mechanical-sympathy/CAGuAWdCqwmySqhNkHRKZcouwKf0F3oM%3DHb_byqLwY8D3Hyb-Gg%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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 [email protected].
To view this discussion on the web, visit 
https://groups.google.com/d/msgid/mechanical-sympathy/6d9273f2-986d-4e58-bfe1-f6a5a8e4fc71n%40googlegroups.com.

Reply via email to