Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / August 2003

Tip: Looking for answers? Try searching our database.

volatile fields

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brianos - 21 Aug 2003 11:09 GMT
I am struggling to understand why Volatile fields exist in C#.

The documentation states that they ensure the required order of
processing.. the example given by Microsoft is :

using System;
using System.Threading;
class Test
{
  public static int result;  
  public static volatile bool finished;
  static void Thread2() {
     result = 143;    
     finished = true;
  }
  static void Main() {
     finished = false;
     // Run Thread2() in a new thread
     new Thread(new ThreadStart(Thread2)).Start();
     // Wait for Thread2 to signal that it has a result by setting
     // finished to true.
     for (;;) {
        if (finished) {
           Console.WriteLine("result = {0}", result);
           return;
        }
     }
  }
}
produces the output:

result = 143
In this example, the method Main starts a new thread running the
method Thread2. This method stores a value into a non-volatile field
called result, then stores true in the volatile field finished. The
main thread waits for the field finished to be set to true, then reads
the field result. Since finished has been declared volatile, the main
thread must read the value 143 from the field result. If the field
finished had not been declared volatile, then it would be permissible
for the store to result to be visible to the main thread after the
store to finished, and hence for the main thread to read the value 0
from the field result. Declaring finished as a volatile field prevents
any such inconsistency.

why on earth would the runtime or hardware or operating system change
the execution order??  surely if you put the 'result' assignment
before the 'finished' assignment, you would expect the execution of
them in that order??? I am confused?  Are microsoft saying that we
cannot trust the execution order when dealing with threaded execution?

Brianos
Aaron Queenan - 21 Aug 2003 11:23 GMT
Volatile ensures that a variable does not get cached in a register on the
CPU, so the value can be read from any thread and will return the same
thing.  If the variable wasn't volatile, the optimiser would think that the
variable hadn't changed, and would therefore not bother to re-read the
variable.  'finished' would always be false, so the loop would never end.

BTW the for(;;) loop below is the worst way to wait for a thread to finish.
You should wait on the thread object or use a semaphore.

Regards,
Aaron Queenan.

> I am struggling to understand why Volatile fields exist in C#.
>
[quoted text clipped - 47 lines]
>
> Brianos

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.