Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Memory Consistency Errors

Reply
Thread Tools

Memory Consistency Errors

 
 
Lew
Guest
Posts: n/a
 
      02-14-2007
Lew wrote:

Knute Johnson wrote:
> Lew:
>
> That's an excellent article, thanks for finding that.


Google is my friend.

-Lew
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      02-14-2007
mei wrote:
> Here it is even though it seems it is outdated:
> http://java.sun.com/docs/books/tutor...cy/atomic.html
> "Atomic actions cannot be interleaved, so they can be used without fear
> of thread interference. However, this does not eliminate all need to
> synchronize atomic actions, because memory consistency errors are still
> possible."


They are talking about "happens-before" relationships.

- Lew
 
Reply With Quote
 
 
 
 
mei
Guest
Posts: n/a
 
      02-14-2007
Lew a écrit :
> mei wrote:
>> Here it is even though it seems it is outdated:
>> http://java.sun.com/docs/books/tutor...cy/atomic.html
>> "Atomic actions cannot be interleaved, so they can be used without
>> fear of thread interference. However, this does not eliminate all need
>> to synchronize atomic actions, because memory consistency errors are
>> still possible."

>
> They are talking about "happens-before" relationships.
>
> - Lew


Ok, you are right: As far as I understand it, what the Sun tutorial
explains about the happens-before relationship matches with what is said
in the IBM article. Thus the tutorial is also up-to-date.
But still the tutorial claims that this new semantic of volatile is not
enough to prevent from memory consistency errors. So I think my initial
question is still unanswered, or am I missing something?
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-14-2007
mei wrote:
> As far as I understand it, what the Sun tutorial
> explains about the happens-before relationship matches with what is said
> in the IBM article. Thus the tutorial is also up-to-date.
> But still the tutorial claims that this new semantic of volatile is not
> enough to prevent from memory consistency errors. So I think my initial
> question is still unanswered, or am I missing something?


See Chris Uppal's answer in this thread. It includes an example of what might
be inconsistent.

- Lew
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      02-15-2007
Chris Uppal wrote:
> Lew wrote:
>
>> According to
>> <http://www-128.ibm.com/developerworks/java/library/j-jtp03304/>

> [...]
>> "The JSR 133 Expert Group decided that it would be more sensible for
>> volatile reads and writes not to be reorderable with any other memory
>> operations ... Under the new memory model, when thread A writes to a
>> volatile variable V, and thread B reads from V, any variable values that
>> were visible to A at the time that V was written are guaranteed now to be
>> visible to B."

>
> Can anyone point me to the passages in JLS3 which imply that. My own study of
> the new stuff has been patchy (and reluctant), but I haven't yet seen anything
> to make the following wrong. (I'm not claiming it /isn't/ wrong, only asking
> for corroboration, or -- better -- refutation).
>
> With the above caveat. Given a non-local volatile variable:
>
> volatile int[] volatileField;
>
> It seems to me that if one thread does the following:
>
> int[] local = new array[2];
> local[0] = 100;
> local[1] = 200;
> volatileField = local;
>
> then there are only so many things that another thread, subsequently reading
> the volatileField's contents, is guaranteed to see.
>
> I believe the following to be guaranteed:
>
> volatileField is not null.
> volatileField refers to an int[] array of length 2.
> volatileField[0] is either 0 or 100
> volatileField[1] is either 0 or 200
>
> However, I haven't yet found anything to convince me that any of the following
> are illegal:
>
> volatileField[0] == 0 && volatileField[1] == 0
> volatileField[0] == 100 && volatileField[1] == 0
> volatileField[0] == 0 && volatileField[1] == 200
> volatileField[0] == 100 && volatileField[1] == 200
>
> The wording in JLS3 seems to be quite specific about the happens
> before-relationship[*] as it applies to volatiles, and it only seems to refer
> to the value of the volatile /itself/, not to any other actions.
>
> It may be that the later stuff (causality, etc) in the JLS3 clears this up, but
> as far at the HB relation goes, I can't see that the visible contents of the
> array are fully constrained.
>
> -- chris
>
>[*] which I think would be better named: must-not-be-seen-to-happen-after.
>
>


I think if you look at this situation as class with two int fields
rather than an int[] you will see that the fields are not volatile
unless declared so. So while the variable volatileField would indeed
reference the correct int[] there would be no synchronization of the
array elements across threads.

Or at least that is what I think today . Then again it could all be
different with arrays.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      02-15-2007
Knute Johnson wrote:
> Chris Uppal wrote:
>> Lew wrote:
>>
>>> According to
>>> <http://www-128.ibm.com/developerworks/java/library/j-jtp03304/>

>> [...]
>>> "The JSR 133 Expert Group decided that it would be more sensible for
>>> volatile reads and writes not to be reorderable with any other memory
>>> operations ... Under the new memory model, when thread A writes to a
>>> volatile variable V, and thread B reads from V, any variable values that
>>> were visible to A at the time that V was written are guaranteed now
>>> to be
>>> visible to B."

>>
>> Can anyone point me to the passages in JLS3 which imply that. My own
>> study of
>> the new stuff has been patchy (and reluctant), but I haven't yet seen
>> anything
>> to make the following wrong. (I'm not claiming it /isn't/ wrong, only
>> asking
>> for corroboration, or -- better -- refutation).
>>
>> With the above caveat. Given a non-local volatile variable:
>>
>> volatile int[] volatileField;
>>
>> It seems to me that if one thread does the following:
>>
>> int[] local = new array[2];
>> local[0] = 100;
>> local[1] = 200;
>> volatileField = local;
>>
>> then there are only so many things that another thread, subsequently
>> reading
>> the volatileField's contents, is guaranteed to see.
>>
>> I believe the following to be guaranteed:
>>
>> volatileField is not null.
>> volatileField refers to an int[] array of length 2.
>> volatileField[0] is either 0 or 100
>> volatileField[1] is either 0 or 200
>>
>> However, I haven't yet found anything to convince me that any of the
>> following
>> are illegal:
>>
>> volatileField[0] == 0 && volatileField[1] == 0
>> volatileField[0] == 100 && volatileField[1] == 0
>> volatileField[0] == 0 && volatileField[1] == 200
>> volatileField[0] == 100 && volatileField[1] == 200
>>
>> The wording in JLS3 seems to be quite specific about the happens
>> before-relationship[*] as it applies to volatiles, and it only seems
>> to refer
>> to the value of the volatile /itself/, not to any other actions.
>>
>> It may be that the later stuff (causality, etc) in the JLS3 clears
>> this up, but
>> as far at the HB relation goes, I can't see that the visible contents
>> of the
>> array are fully constrained.
>>
>> -- chris
>>
>>[*] which I think would be better named:
>> must-not-be-seen-to-happen-after.
>>
>>

>
> I think if you look at this situation as class with two int fields
> rather than an int[] you will see that the fields are not volatile
> unless declared so. So while the variable volatileField would indeed
> reference the correct int[] there would be no synchronization of the
> array elements across threads.
>
> Or at least that is what I think today . Then again it could all be
> different with arrays.
>


And I found some more interesting things. Look at AtomicBoolean in the
docs and follow the link 'java.util.concurrent.atomic'. It has a
discussion of the Atomic??? classes and especially see
AtomicIntegerArray. I still think that it would require synchronization
to prevent memory consistency errors however.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory consistency error in multithreaded program? Knute Johnson Java 4 01-30-2007 01:56 AM
Resource (.Resx) file consistency checker prabhupr@hotmail.com ASP .Net 1 11-17-2006 12:36 AM
Maintain Consistency With ASP.NET Templates =?Utf-8?B?VkIgQ29kZXI=?= ASP .Net 1 02-13-2006 10:38 PM
Consistency? decius The Lounge 3 06-26-2005 05:28 PM
Errors, errors, errors Mark Goldin ASP .Net 2 01-17-2004 08:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57