Thursday, January 29, 2015

Difference between Thread.Sleep and Thread.SpinWait in C# .NET

Thread.Sleep:

Thread.Sleep method used to put a thread to sleep for a specified timeout and thread will be put in special queue of threads for waiting. Contextswitching will happen and other thread will get chance to process. After specified time elapsed, slept thread will be again awakened by operationg system.

Example:
Thread.Sleep(10); //Thread will sleep for 10 milliseconds


Thread.SpinWait

It will wait thread by specifying iteration[CPU] count.

Example:
Thread.SpinWait(10000); //Thread will wait for 10000 iterations to complete


As per example, current thred will wait for 10000 iteration to complete. Time required will be depend on CPU processing power.

No comments:

Post a Comment