Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What is the purpose of volatile keyword in C#?

Where would I need to use this keyword?

I saw the following statement, but I am unable to understand why volatile is required here?

internal volatile string UserName; 
like image 930
Zain Shaikh Avatar asked Nov 05 '10 03:11

Zain Shaikh


People also ask

What is volatile variable in C where it is used?

Volatile is a qualifier that is applied to a variable when it is declared. It tells the compiler that the value of the variable may change at any time-without any action being taken by the code the compiler finds nearby. The implications of this are quite serious.

What are volatile variables and what is their purpose?

A volatile variable is a variable that is marked or cast with the keyword "volatile" so that it is established that the variable can be changed by some outside factor, such as the operating system or other software.


2 Answers

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

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 (§8.12). These optimizations can be performed by the compiler, by the run-time system, or by hardware. For volatile fields, such reordering optimizations are restricted:

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.

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.

Read that extremely carefully if you have any intention of ever making a volatile field. If you do not completely and thoroughly understand all the implications of volatile semantics then do not attempt to use them. It is usually far better to use a lock, which automatically gives you sufficient memory barriers to ensure the necessary acquire and release semantics. Remember, locks are only really expensive when they are contended.

like image 87
Eric Lippert Avatar answered Oct 04 '22 08:10

Eric Lippert


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 not ever cache the variable, but to instead be sure to read it before every use.

An example of something that would be volatile would be a hardware register that your code has memory mapped and is reading to determine when a flag is set. The hardware may set the value while your code is running and without using the volatile keyword you would not notice this change as the assembly would not ever actually check the value.

like image 22
w.donahue Avatar answered Oct 04 '22 08:10

w.donahue