Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Memory Consistency Errors

 
 
mei
Guest
Posts: n/a
 
      02-13-2007
Hi,
While reading the tutorial
http://java.sun.com/docs/books/tutor...ncy/index.html,
something caught my attention:
It is said that memory consistency errors can occur even when atomicity
of reading/writing access is guaranteed with volatile.
However they don't develop this matter.
Is there somebody that could explain me more how could this happen?
Thank in advance.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      02-13-2007
mei wrote On 02/13/07 14:26,:
> Hi,
> While reading the tutorial
> http://java.sun.com/docs/books/tutor...ncy/index.html,
> something caught my attention:
> It is said that memory consistency errors can occur even when atomicity
> of reading/writing access is guaranteed with volatile.
> However they don't develop this matter.
> Is there somebody that could explain me more how could this happen?
> Thank in advance.


Accesses to main memory might not occur in the same
order that the CPU initiated them, particularly for writes
(which often go through hardware write buffers so the CPU
needn't wait for them). If CPU 1 writes the Answer to
location A and then writes the AnswerIsReady flag to B,
CPU 2 may see the change to B before it sees the change
to A, and thus get the WrongAnswer. Making either or both
writes atomic doesn't help; what's needed is something
called a "memory barrier."

--

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      02-14-2007
mei wrote:
> Hi,
> While reading the tutorial
> http://java.sun.com/docs/books/tutor...ncy/index.html,
> something caught my attention:
> It is said that memory consistency errors can occur even when atomicity
> of reading/writing access is guaranteed with volatile.
> However they don't develop this matter.
> Is there somebody that could explain me more how could this happen?
> Thank in advance.


I looked and couldn't find the reference you mentioned. Do you have the
URL of the page you saw that on?

Thanks,

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      02-14-2007
Eric Sosman wrote:
> mei wrote On 02/13/07 14:26,:
>> Hi,
>> While reading the tutorial
>> http://java.sun.com/docs/books/tutor...ncy/index.html,
>> something caught my attention:
>> It is said that memory consistency errors can occur even when atomicity
>> of reading/writing access is guaranteed with volatile.
>> However they don't develop this matter.
>> Is there somebody that could explain me more how could this happen?
>> Thank in advance.

>
> Accesses to main memory might not occur in the same
> order that the CPU initiated them, particularly for writes
> (which often go through hardware write buffers so the CPU
> needn't wait for them). If CPU 1 writes the Answer to
> location A and then writes the AnswerIsReady flag to B,
> CPU 2 may see the change to B before it sees the change
> to A, and thus get the WrongAnswer. Making either or both
> writes atomic doesn't help; what's needed is something
> called a "memory barrier."
>


Is that not what you get with volatile?

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-14-2007
mei wrote:
>> Hi,
>> While reading the tutorial
>> http://java.sun.com/docs/books/tutor...ncy/index.html,
>> something caught my attention:
>> It is said that memory consistency errors can occur even when
>> atomicity of reading/writing access is guaranteed with volatile.
>> However they don't develop this matter.
>> Is there somebody that could explain me more how could this happen?
>> Thank in advance.


Knute Johnson wrote:
> I looked and couldn't find the reference you mentioned. Do you have the
> URL of the page you saw that on?


According to
<http://www-128.ibm.com/developerworks/java/library/j-jtp03304/>

"The original semantics for volatile guaranteed only that reads and writes of
volatile fields would be made directly to main memory, instead of to registers
or the local processor cache, ... In other words, this means that the old
memory model made promises only about the visibility of the variable being
read or written, and no promises about the visibility of writes to other
variables. While this was easier to implement efficiently, it turned out to be
less useful than initially thought.
"...
"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."

So it seems that what used to be is now better.

- Lew
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      02-14-2007
Knute Johnson wrote:
> Eric Sosman wrote:
>> mei wrote On 02/13/07 14:26,:
>>> Hi,
>>> While reading the tutorial
>>> http://java.sun.com/docs/books/tutor...ncy/index.html,
>>>
>>> something caught my attention:
>>> It is said that memory consistency errors can occur even when
>>> atomicity of reading/writing access is guaranteed with volatile.
>>> However they don't develop this matter.
>>> Is there somebody that could explain me more how could this happen?
>>> Thank in advance.

>>
>> Accesses to main memory might not occur in the same
>> order that the CPU initiated them, particularly for writes
>> (which often go through hardware write buffers so the CPU
>> needn't wait for them). If CPU 1 writes the Answer to
>> location A and then writes the AnswerIsReady flag to B,
>> CPU 2 may see the change to B before it sees the change
>> to A, and thus get the WrongAnswer. Making either or both
>> writes atomic doesn't help; what's needed is something
>> called a "memory barrier."
>>

>
> Is that not what you get with volatile?


Hmmm... <browses the specifications, scratches head, gets
splinters under fingernails>. Perhaps it is. It seems the
precise meaning of `volatile' has undergone some changes, and
that the memory-ordering semantics are now tighter than they
used to be. (See Lew's post on this thread for an informative
quote.)

mei, my answer is correct (I think) for older Java versions,
but is wrong for the latest versions. I apologize for the
out-of-date information.

--
Eric Sosman
lid

 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      02-14-2007
Lew wrote:
> mei wrote:
>>> Hi,
>>> While reading the tutorial
>>> http://java.sun.com/docs/books/tutor...ncy/index.html,
>>>
>>> something caught my attention:
>>> It is said that memory consistency errors can occur even when
>>> atomicity of reading/writing access is guaranteed with volatile.
>>> However they don't develop this matter.
>>> Is there somebody that could explain me more how could this happen?
>>> Thank in advance.

>
> Knute Johnson wrote:
>> I looked and couldn't find the reference you mentioned. Do you have
>> the URL of the page you saw that on?

>
> According to
> <http://www-128.ibm.com/developerworks/java/library/j-jtp03304/>
>
> "The original semantics for volatile guaranteed only that reads and
> writes of volatile fields would be made directly to main memory, instead
> of to registers or the local processor cache, ... In other words, this
> means that the old memory model made promises only about the visibility
> of the variable being read or written, and no promises about the
> visibility of writes to other variables. While this was easier to
> implement efficiently, it turned out to be less useful than initially
> thought.
> "...
> "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."
>
> So it seems that what used to be is now better.
>
> - Lew


Lew:

That's an excellent article, thanks for finding that.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      02-14-2007
Eric Sosman wrote:
> Knute Johnson wrote:
>> Eric Sosman wrote:
>>> mei wrote On 02/13/07 14:26,:
>>>> Hi,
>>>> While reading the tutorial
>>>> http://java.sun.com/docs/books/tutor...ncy/index.html,
>>>>
>>>> something caught my attention:
>>>> It is said that memory consistency errors can occur even when
>>>> atomicity of reading/writing access is guaranteed with volatile.
>>>> However they don't develop this matter.
>>>> Is there somebody that could explain me more how could this happen?
>>>> Thank in advance.
>>>
>>> Accesses to main memory might not occur in the same
>>> order that the CPU initiated them, particularly for writes
>>> (which often go through hardware write buffers so the CPU
>>> needn't wait for them). If CPU 1 writes the Answer to
>>> location A and then writes the AnswerIsReady flag to B,
>>> CPU 2 may see the change to B before it sees the change
>>> to A, and thus get the WrongAnswer. Making either or both
>>> writes atomic doesn't help; what's needed is something
>>> called a "memory barrier."
>>>

>>
>> Is that not what you get with volatile?

>
> Hmmm... <browses the specifications, scratches head, gets
> splinters under fingernails>. Perhaps it is. It seems the
> precise meaning of `volatile' has undergone some changes, and
> that the memory-ordering semantics are now tighter than they
> used to be. (See Lew's post on this thread for an informative
> quote.)
>
> mei, my answer is correct (I think) for older Java versions,
> but is wrong for the latest versions. I apologize for the
> out-of-date information.
>


Thanks Eric.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      02-14-2007
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.


 
Reply With Quote
 
mei
Guest
Posts: n/a
 
      02-14-2007
Knute Johnson a écrit :
> mei wrote:
>> Hi,
>> While reading the tutorial
>> http://java.sun.com/docs/books/tutor...ncy/index.html,
>> something caught my attention:
>> It is said that memory consistency errors can occur even when
>> atomicity of reading/writing access is guaranteed with volatile.
>> However they don't develop this matter.
>> Is there somebody that could explain me more how could this happen?
>> Thank in advance.

>
> I looked and couldn't find the reference you mentioned. Do you have the
> URL of the page you saw that on?
>
> Thanks,
>


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."
 
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