多线程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;
}
}
{
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的主要内容,如果未能解决你的问题,请参考以下文章