473,466 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Volatile & ECMA Specification

Section 17.4.3 of the ECMA-334 C# Language Specification says

1 When a field-declaration includes a volatile modifier, the fields introduced by that declaration are volatile fields. 2 For non-volatile fields, optimization techniques that reorder instructions can lead to unexpected and unpredictable results in multi-threaded programs that access fields without synchronization such as that provided by the lock-statement (15.12). 3 These optimizations can be performed by the compiler, by the runtime system, or by hardware. 4 For volatile fields, such reordering optimizations are restricted: 5 A read of a volatile field is called a volatile read. 6 A volatile read has "acquire semantics"; that is, it is guaranteed to occur prior to any references to memory that occur after it in the instruction sequence. 7 A write of a volatile field is called a volatile write. 8 A volatile write has "release semantics"; that is, it is guaranteed to happen after any memory references prior to the write instruction in the instruction sequence.

A literal reading of part 6 is that a read of a volatile field "x" is guaranteed to occur before all other references to memory after the read in the instruction sequence, including references to fields other than "x"

Similarly, a literal reading of part 7 is that a write to a volatile field "x" is guaranteed to happen after all memory references prior to the write in the instruction sequence, including references to fields other than "x"

Are the literal readings correct, or are the guarantees only with respect to a single field at one time ("x")? For example, suppose we have two field

volatile int x
int y

and y is initialized to 0, a long time goes by, and some thread A executes this

y = 3

and then a different thread B executes this

x = x + 1
Console.WriteLine("x={0} y={1}", x, y)

Is guaranteed that the value printed for y is 3
Jul 21 '05 #1
4 1892
Andrew,

Can you drop me an email at Er****@microsoft.com with this question, and
I'll try to find you an answer.

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"Andrew" <an*******@discussions.microsoft.com> wrote in message
news:10**********************************@microsof t.com...
Section 17.4.3 of the ECMA-334 C# Language Specification says:

1 When a field-declaration includes a volatile modifier, the fields introduced by that declaration are volatile fields. 2 For non-volatile
fields, optimization techniques that reorder instructions can lead to
unexpected and unpredictable results in multi-threaded programs that access
fields without synchronization such as that provided by the lock-statement
(15.12). 3 These optimizations can be performed by the compiler, by the
runtime system, or by hardware. 4 For volatile fields, such reordering
optimizations are restricted: 5 A read of a volatile field is called a
volatile read. 6 A volatile read has "acquire semantics"; that is, it is
guaranteed to occur prior to any references to memory that occur after it in
the instruction sequence. 7 A write of a volatile field is called a
volatile write. 8 A volatile write has "release semantics"; that is, it is
guaranteed to happen after any memory references prior to the write
instruction in the instruction sequence.
A literal reading of part 6 is that a read of a volatile field "x" is guaranteed to occur before all other references to memory after the read in
the instruction sequence, including references to fields other than "x".
Similarly, a literal reading of part 7 is that a write to a volatile field "x" is guaranteed to happen after all memory references prior to the write
in the instruction sequence, including references to fields other than "x".
Are the literal readings correct, or are the guarantees only with respect to a single field at one time ("x")? For example, suppose we have two
fields
volatile int x;
int y;

and y is initialized to 0, a long time goes by, and some thread A executes this:
y = 3;

and then a different thread B executes this:

x = x + 1;
Console.WriteLine("x={0} y={1}", x, y);

Is guaranteed that the value printed for y is 3?

Jul 21 '05 #2
Done. Thanks.

Jul 21 '05 #3
Andrew <an*******@discussions.microsoft.com> wrote:

<snip>
Are the literal readings correct, or are the guarantees only with
respect to a single field at one time ("x")? For example, suppose we
have two fields

volatile int x;
int y;

and y is initialized to 0, a long time goes by, and some thread A
executes this:

y = 3;

and then a different thread B executes this:

x = x + 1; Console.WriteLine("x={0} y={1}", x, y);

Is guaranteed that the value printed for y is 3?


Yes, I believe that's guaranteed - I believe your literal readings are
correct. I'll be interested to hear what response you get back from
Eric though - please post it!

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
Here's what Chris Brumme sent me

-

From cb*****@microsoft.com Thu Feb 12 07:43:42 200

Your literal readings of part 6 & 7 are correct

In your example, assuming that thread B performs the update to x >after< the other thread assigns 3 to y, then you are guaranteed that thread B will see the value 3. Of course, the way you show it there is no explicit synchronization. So I just have to believe you when you say the other thread has already made the assignment

If you want to read about the differences between the ECMA .NET memory model and the CLR's memory model (which is much stronger and which therefore doesn't require volatile for the example you show), have a look at http://blogs.msdn.com/cbrumme/archiv.../17/51445.aspx. In the CLR, all stores have release semantics (even without volatile). Reads do not have acquire semantics unless you use volatile

Chris
http://blogs.msdn.com/cbrumm
Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Florian Proch | last post by:
Hi all... i'm currently working for a big project to construct a website. We need to support some browser and OS : IE 5.0 -> IE 6, Mozilla 1.2 -> 1.7, Opera 5 -> Opéra 7.5x, Netscape 4.75 -> NS...
5
by: ben | last post by:
Hello All, I am trying to make sense of a bit of syntax, is there a guru out there that can clear this up for me. I have a buffer declared as static volatile u8 buffer; and I have a...
6
by: Tony Whitter | last post by:
Does anyone know how much changed between the final draft dated October 2002 and the Ecma-334 C# Language Specification 2nd edition dated December 2002 and if there is a change history document...
22
by: Assaf | last post by:
hi all i know that i should not cross-post, but i am not sure to which group to post this question. 2 quesions about volatile: 1. i use volatile when 2 threads access the same variable...
14
by: Pierre | last post by:
Using the "volatile" keyword, creates a problem if I intend to use any of the interlocked APIs. The compiler generates an error if I use the following line, for example: ...
4
by: Andrew | last post by:
Section 17.4.3 of the ECMA-334 C# Language Specification says 1 When a field-declaration includes a volatile modifier, the fields introduced by that declaration are volatile fields. 2 For...
9
by: Edward Diener | last post by:
Are there any differences between the version of C++/CLI as implemented in Visual C++ 2005 and the ECMA-372 C++/CLI Language Specification of December 2005 freely downloaded from ECMA ? Asking...
28
by: Andre | last post by:
Hi, Does anyone know whether the ECMA, or an other standard document, specifies a maximum for the value that can be pass to the setTimeOut() function in Javascript? Andre
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.