On Wed, Sep 14, 2022 at 1:51 PM r r <grosi...@gmail.com> 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 mechanical-sympathy+unsubscr...@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 > <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 mechanical-sympathy+unsubscr...@googlegroups.com. To view this discussion on the web, visit https://groups.google.com/d/msgid/mechanical-sympathy/CAGuAWdCqwmySqhNkHRKZcouwKf0F3oM%3DHb_byqLwY8D3Hyb-Gg%40mail.gmail.com.