多线程16-SpinWait

    class Program
    {
        static volatile bool isCompleted = false;
        static void UserModeWait()
        {
            while(!isCompleted)
            {
                Console.Write(".");
            }
            Console.WriteLine();
            Console.WriteLine("waiting is complete");
        }
        static void HybridSpinWait()
        {
            var w = new SpinWait();
            while(!isCompleted)
            {
                w.SpinOnce();
                Console.WriteLine(w.NextSpinWillYield);

            }
            Console.WriteLine("waiting is complete");
        }
        static void Main()
        {
            var t1 = new Thread(UserModeWait);
            var t2 = new Thread(HybridSpinWait);
            Console.WriteLine("running user mode waiting");
            t1.Start();
            Thread.Sleep(20);
            isCompleted = true;
            Thread.Sleep(TimeSpan.FromSeconds(1));
            isCompleted = false;
            Console.WriteLine("running hybrid soinwait construct waiting ");
            t2.Start();
            Thread.Sleep(5);
            isCompleted = true;
        }
    }
posted @ 2016-06-23 08:38  shidengyun  阅读(236)  评论(0编辑  收藏  举报