コード例 #1
0
        private static void SomeThread(object o1)
        {
            VolatileClass o = (VolatileClass)o1;

            Console.WriteLine("Step 1: Entered the loop");
            while (o._loop)
            {
            }
            Console.WriteLine("Step 3: Exited the loop");
        }
コード例 #2
0
        private volatile bool _loop = true;   //Check output with and without volatile keyword
        public static void Method()
        {
            VolatileClass test1 = new VolatileClass();
            Thread        obj   = new Thread(SomeThread);

            obj.Start(test1);

            Thread.Sleep(20);

            test1._loop = false;
            Console.WriteLine("Step 2: Value is set to false");
        }