码迷,mamicode.com
首页 > 编程语言 > 详细

多线程16-SpinWait

时间:2016-06-23 08:53:29      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

    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;
        }
    }

多线程16-SpinWait

标签:

原文地址:http://www.cnblogs.com/shidengyun/p/5609265.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!