What is the purpose of the volatile keyword in C #

What is the purpose of the volatile keyword in C #?

Where do I need to use this keyword?

I saw the following statement, but I can’t understand why volatile is required here?

 internal volatile string UserName; 
+17
multithreading c # volatile
Nov 05 '10 at 3:39
source share
6 answers

I refer you to section 10.5.3 of the specification, which states:

For non-volatile fields, optimization of methods that change the order of instructions can lead to unexpected and unpredictable results in multithreaded programs that have fields without synchronization, such as those that are provided using the lock operator (§8.12). These optimizations may be performed by the compiler, a runtime system, or hardware. For volatile fields, such reordering optimization is limited:

and reading a volatile field is called unstable reading. Unstable reading "acquire semantics"; that is, guaranteed to happen before memory references that occur after this in a sequence of commands.

and the record of a volatile field is called volatile write. Unstable record "release semantics"; that is, it is guaranteed to happen after any memory link before writing the instruction in the instruction sequence.

These restrictions ensure that all threads will observe mutable records executed by any other thread in the order in which they were executed. The corresponding implementation is not required to provide a single general order of volatility is recorded as seen from all execution threads.

Read this very carefully if you intend to ever make a volatile field. If you do not fully and completely understand all the consequences of volatile semantics, then do not try to use them. It is usually much better to use a lock that automatically gives you sufficient memory barriers to provide the necessary semantics for receiving and releasing. Remember that castles are really expensive when they fight.

+28
Nov 05 '10 at 5:59
source share

Volatile is used for a variable that can change without your action while your code is running. It tells the compiler to write the assembly in such a way as to never cache this variable, but be sure to read it before each use instead.

An example of what will be volatile is the hardware register, which your code has a memory card and reads to determine when the flag is set. The hardware can set the value while your code is running, and without using the volatile keyword you will not notice this change since the assembly will never check the value.

+7
Nov 05 '10 at 3:42
source share

Volatile is a hint to the compiler (and the ngen / jit compiler) that the value of this variable can change at any time, and thus optimization around accessing the variable by caching the value locally volatile will be disabled.

Consider the following code:

 If (UserName == "") // do something If (UserName == "Fred") // do something 

If volatile was not present, the compiler can generate an IL, where it stores the link on the stack for the first comparison and repeats it for the second. However, adding volatile tells the compiler that the link can be changed by another thread, causing it to generate an IL that will not reuse a copy of the stack from the first comparison.

+6
Nov 05 '10 at 3:54
source share

MSDN will summarize better than me ....

"The volatile keyword indicates that a field can be modified by multiple threads that are running at the same time. Fields declared by volatile do not fall under compiler optimization that assume access by a single thread. An exact value is always present in the field."

http://msdn.microsoft.com/en-us/library/x13ttww7(v=VS.100).aspx

+4
Nov 05 '10 at 3:42
source share

This means that the value can be changed by another thread, so the value must be read even if the previous instruction has already read it.

http://msdn.microsoft.com/en-us/library/x13ttww7%28VS.71%29.aspx

+3
Nov 05 2018-10-11T00:
source share

It is nothing but telling the compiler that this variable will change its value at any time with anything and that the compiler should not make any assumptions about this variable.

Typically, the compiler assumes that some variable will be constant at runtime. This can lead to an error checking registor several times. Because the value of the register can change anything. So, for these variables it should be declared “mutable” and checked every time it appears in the code without any assumptions.

+2
Nov 05 2018-10-11T00
source share



All Articles