473,378 Members | 1,482 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

C# volatile field

In this example, what would have happened, had the field "result" not
been declared "volatile"?

http://msdn.microsoft.com/library/de...pec_10_4_3.asp
The last few lines on the page tell us that but I do not understand.
Can someone please explain clearly?

Nov 17 '05 #1
2 2218
Water Cooler v2 <wt*****@yahoo.com> wrote:
In this example, what would have happened, had the field "result" not
been declared "volatile"?

http://msdn.microsoft.com/library/de...library/en-us/
csspec/html/vclrfcsharpspec_10_4_3.asp
The last few lines on the page tell us that but I do not understand.
Can someone please explain clearly?


See http://www.pobox.com/~skeet/csharp/t...latility.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
On 3 Sep 2005 22:18:37 -0700, "Water Cooler v2" <wt*****@yahoo.com>
wrote:
In this example, what would have happened, had the field "result" not
been declared "volatile"?

http://msdn.microsoft.com/library/de...pec_10_4_3.asp
The last few lines on the page tell us that but I do not understand.
Can someone please explain clearly?

The example has two threads, Main and Thread2. Look at part of the
code in Main on its own:

finished = false;

for (;;) {
if (finished) {
Console.WriteLine("result = {0}", result);
return;
}
}

An optimising compiler might realise that the variable "finished" is
not changed and could be replaced by a literal:

for (;;) {
if (false) {
Console.WriteLine("result = {0}", result);
return;
}
}

If the code looked like this then it would never terminate as it could
be further optimised to:

for (;;) {
}
By declaring the variable "finished" as "volatile" you give notice to
the compiler that the variable "finished" might be changed at any time
by another thread, in this case Thread2. This means that the compiler
will not optimise away the variable, or do any other optimisations
that might prevent the code from working.

HTH

rossum
The ultimate truth is that there is no ultimate truth
Nov 17 '05 #3

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

Similar topics

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...
2
by: spammy | last post by:
hi all, im trying to establish whether i have a race condition or critical section in the following. i have a dataaccess class that continually retireves a table from a sqlserver (which may be...
15
by: DanGo | last post by:
Hi All, I'm trying to get my head around synchronization. Documentation seems to say that creating a volatile field gives a memorybarrier. Great! But when I do a little performance testing...
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: ...
13
by: yaron | last post by:
Hi all, let be focus on sigle processor machine 32 bits. 1. with multi-threaded on single processor machine 32bit do i have to sync access to atomic get/set properties of type less then 32 bits...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.