[concurrency-interest] Relativity of guarantees provided by volatile

Marko Topolnik mtopolnik at inge-mark.hr
Sat Aug 18 14:11:23 EDT 2012


Jon,

the only thing you need to notice is that the rows in your table are sequenced by the synchronization order, which has nothing (at least formally) to do with the wall-clock timing order of the events, and especially: you show a write as a single event where in fact it is two events separated in time: the moment Thread0 writes a value and a later moment when Thread1 observes it is written. That later moment can be arbitrarily postponed as long as the overall happens-before is maintained. This can in the end create the behavior that I demonstrate in my original post.

That is why the JMM **does not** guarantee that Thread1 will observe at t=3 a write by Thread0 at t=0. It only guarantees that **when** it observes it, it will definitely also observe any writes preceding it, so it will see the actions of Thread0 as if they happened in program order.

-Marko

On 18. kol. 2012., at 18:13, Jan Nielsen wrote:

> On Fri, Aug 17, 2012 at 3:24 PM, Marko Topolnik <mtopolnik at inge-mark.hr> wrote:
> 2. If there is no loophole, is there anything to worry about, given that practically 100% developers out there consider as guaranteed something that isn't?
> 
> I'm part of the 99%. When I read: "A field may be declared volatile, in which case the Java Memory Model ensures that all threads see a consistent value for the variable" [1] I understand this to mean for volatile variable 's' updated in one thread and read from another, these scenarios are possible:
> 
>    Thread0
> 0: volatile int s=0;
> 
>    Thread0  Thread1
> -----------------
> 1: s=1
> 2:          s==1
> 3: s=2
> 4:          s==2
> 5: s=3
> 6:          s==3
> -----------------
> 1:          s==1
> 2:          s==1
> 3:          s==1
> 4: s=1
> 5: s=2
> 6: s=3
> -----------------
> 1: s=1
> 2: s=2
> 3: s=3
> 4:          s==3
> 5:          s==3
> 6:          s==3
> 
> [1] http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.1.4
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest




More information about the Concurrency-interest mailing list