Thanks!

czw., 15 wrz 2022 o 12:40 Peter Veentjer <[email protected]> napisał(a):

>
>
> On Thu, Sep 15, 2022 at 11:43 AM r r <[email protected]> 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.
>
>
>>
>> ś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
>> <https://groups.google.com/d/msgid/mechanical-sympathy/6d9273f2-986d-4e58-bfe1-f6a5a8e4fc71n%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/CAGuAWdAR%3DyDoeSXvDfmc18OcpHJ5dufDFnrfzY0mZiYyRR9Vjw%40mail.gmail.com
> <https://groups.google.com/d/msgid/mechanical-sympathy/CAGuAWdAR%3DyDoeSXvDfmc18OcpHJ5dufDFnrfzY0mZiYyRR9Vjw%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/CAFKLJkxRSV2a7Y_fePB135THa2bqZ2ZkpKGTGVwP78FzmXpPPQ%40mail.gmail.com.

Reply via email to