Skip to content

Instantly share code, notes, and snippets.

Created December 13, 2012 13:57
Show Gist options
  • Save anonymous/4276529 to your computer and use it in GitHub Desktop.
Save anonymous/4276529 to your computer and use it in GitHub Desktop.
Sleep(1) x Sleep(0)
// Code From http://www.bluebytesoftware.com/blog/PermaLink,guid,1c013d42-c983-4102-9233-ca54b8f3d1a1.aspx
using System;
using System.Diagnostics;
using System.Threading;
class Program {
public static volatile int x = 0;
public static void Main() {
Stopwatch sw = new Stopwatch();
sw.Start();
SpawnWork();
while (x == 0) {
Thread.Sleep(0);
}
sw.Stop();
Console.WriteLine("Sleep(0) = {0}", sw.Elapsed);
x = 0;
sw.Reset();
sw.Start();
SpawnWork();
while (x == 0) {
Thread.Sleep(1);
}
sw.Stop();
Console.WriteLine("Sleep(1) = {0}", sw.Elapsed);
}
private static void SpawnWork() {
ThreadPool.QueueUserWorkItem(delegate {
Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;
x = 1;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment