多线程16-SpinWait

Posted shidengyun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程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;
        }
    }

以上是关于多线程16-SpinWait的主要内容,如果未能解决你的问题,请参考以下文章

并发编程之多线程

什么是多线程,多进程?

多线程和多进程模式有啥区别

多线程Java多线程学习笔记 | 多线程基础知识

java中啥叫做线程?啥叫多线程?多线程的特点是啥

c++ 多线程与c多线程有啥区别?