user1570094
user1570094

Reputation: 197

volatile keyword usage

I am trying to understand the usage of volatile keyword, so I have written a small example where I was thinking to use Volatile keyword, but currently I am getting the same behavior either I use Volatile keyword or I don't use it.

Following is the code which I am running and I expected the Thread t2 keep executing, even if t1 was updating the ExitLoop property

namespace UsageOfVolatileKeyword
{
    class Program
    {
        static void Main()
        {
            Test t = new Test();

            Thread t2 = new Thread(() => { while (!t.ExitLoop) { Console.WriteLine("In loop"); } });
            t2.Start();

    Thread t1 = new Thread(() => t.ExitLoop = true);
            t1.Start();

            t2.Join();

            Console.WriteLine("I am done");

            Console.ReadLine();
        }
    }

    class Test
    {
        private bool _exitLoop = false; //I am marking this variable as volatile.

        public bool ExitLoop
        {
            get { return _exitLoop; }
            set { _exitLoop = value; }
        }
    }
}

It will be nice if somebody can help me understand what wrong I am doing and what's the proper usage of Volatile keyword is.

Upvotes: 2

Views: 232

Answers (3)

Brian Gideon
Brian Gideon

Reputation: 48949

The reason why you do not observe a difference is because Console.WriteLine already generates a memory barrier making volatile redundant in this particular scenario. It is also critical that you do these kinds of tests using a release build and running the application without the debugger attached. Look at my answers here and here, but especially here for examples and explanations on how to reproduce this.

Upvotes: 1

Spence
Spence

Reputation: 29332

At a high level, volatile is a marker to the compiler "Make no assumptions about the state of this variable". Essentially you would use it to indicate that either multiple threads or perhaps part of the computer itself might be changing this memory location. For example you might map something to the UART data register on your serial port. When you read this variable, it's value is whatever is in the hardware.

If you don't tell the compiler that this variable is volatile, it might assume that if you set it to true and then use it some point later in the code, that this variable is still true and use some constant folding logic to optimise the code.

Upvotes: 3

bhaskar
bhaskar

Reputation: 38

Have you referred http://msdn.microsoft.com/en-us/library/x13ttww7(v=vs.71).aspx ?

variable mentioned with volatile to get no-cached value according recent CPU instruction and no compiler optimization during compilation.

In your case, I think CPU did context switch effectively and flag was consistent (with/out flag). I think we can see some different when there are many multiple threads and cpu was engaged in switching

You also refer the following thread Why is volatile needed in C? which referred in c about OS level internals

Upvotes: 1

Related Questions