Hello Friends,

Today while doing a POC on Signal R I came across a keyword “Volatile” that I had never used before.

It came up in a class level field declaration

private volatile bool _updatingStockPrices = false; 

A quick reference to the MSDN reveled that Volatile keyword should be used in Class or Struct level fields to indicate that this field might be modified by multiple threads that are being executed at the same time.

Fields those are marked as volatile will not be subjected to compiler optimization which assume that the field may only be accessed by a Single thread.

This ensures that the most up-to-date value is available in this field all the times.

I will do a sample application that demonstrates the issues that may occur with or without using volatile fields.

More details on MSDN article here

http://msdn.microsoft.com/en-us/library/x13ttww7.aspx

Next Recommended Reading
Remove AM PM from Time String using C#