C# Source Code: Using the "Volatile" variable modifier in multithreaded applications
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
Using the "Volatile" variable modifier in multithreaded applications
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, July 21, 2004
Hits:
840
Category:
Threading/Asynchronous operations
Article:
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. These optimizations can be performed by the compiler, by the runtime system, or by hardware. For volatile fields, such reordering optimizations are restricted: 1. A read of a volatile field is called a volatile read. 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. 2. A write of a volatile field is called a volatile write. 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. These restrictions ensure that all threads will observe volatile writes performed by any other thread in the order in which they were performed. A conforming implementation is not required to provide a single total ordering of volatile writes as seen from all threads of execution. The type of a volatile field must be one of the following: A reference-type. The type byte, sbyte, short, ushort, int, uint, char, float, or bool. An enum-type having an enum base type of byte, sbyte, short, ushort, int, or uint In the example below, the method Main starts a new thread which calls the method ThreadToReturnFinishedCode. This method stores a value into a non-volatile field called "returnCode", 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 "returnCode". Since "finished" has been declared volatile, the main thread must read the value 1 from the field "returnCode". If the field "finished" had not been declared volatile, then it would be permissible for the write to the "returnCode" field to only be visible to the main thread AFTER the write to "finished". Hence the main thread would read the value 0 from the "returnCode" field, instead of the correct value 1. Declaring finished as a volatile field prevents any such inconsistency. using System; using System.Threading; class VolatileTest { //Normal static int field (contains return code) public static int returnCode = 0; //Volatile boolean field public static volatile bool finished = false; static void ThreadToReturnFinishedCode() { returnCode = 1; finished = true; } static void Main() { //Create thread which sets finished flag to true new Thread(new ThreadStart(ThreadToReturnFinishedCode)).Start(); //Wait for finished flag do { if (finished) { Console.WriteLine("Finished test. Return code = {0}", returnCode); return; } } while(true); //Pause Console.ReadLine(); } }
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet