C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: Because SpinWait is computationally intensive, it will complete earlier or later depending on your computer's clock speed.
C# program that uses Thread.SpinWait
using System;
using System.Diagnostics;
using System.Threading;
class Program
{
static void Main()
{
Stopwatch stopwatch = Stopwatch.StartNew();
// CPU is maxed out during this call.
// ... (Try making it much larger to test.)
Thread.SpinWait(1000000);
Console.WriteLine(stopwatch.Elapsed.TotalMilliseconds);
}
}
Output
3.0908
And: When I used Thread.Sleep, the experiment did not involve the CPU and so did not yield correct results.
But: When I used Thread.SpinLock, the results were correct. SpinLock is necessary to simulate actual CPU usage.
ThreadPool.SetMinThreads