26

As Java has had Sleep and Yield from long ago, I've found answers for that platform, but not for .Net

.Net 4 includes the new Thread.Yield() static method. Previously the common way to hand over the CPU to other process was Thread.Sleep(0).

Apart from Thread.Yield() returning a boolean, are there other performance, OS internals differences?

For example, I'm not sure if Thread.Sleep(0) checks if other thread is ready to run before changing the current Thread to waiting state... if that's not the case, when no other threads are ready, Thread.Sleep(0) would seem rather worse that Thread.Yield().

Community
  • 1
  • 1
Xose Lluis
  • 895
  • 1
  • 9
  • 8

3 Answers3

12

As explained by Eric Lippert in his Coverity blog post “How does locking work in C#?” while demonstrating how to implement locking—

The .NET Framework gives you multiple tools you could use to build a more sophisticated waiting strategy: Thread.SpinWait puts the processor into a tight loop allowing you to wait a few nanoseconds or microseconds without ceding control to another thread. Thread.Sleep(0) cedes control to any ready thread of equal priority or keeps going on the current thread if there is none. Thread.Yield cedes control to any ready thread associated with the current processor. And as we’ve seen, Thread.Sleep(1) cedes control to any ready thread of the operating system’s choice. By carefully choosing a mix of these calls and doing performance testing in realistic conditions you could build a high-performance implementation, and of course this is what the CLR team has actually done.

ib.
  • 27,830
  • 11
  • 80
  • 100
Vipresh
  • 1,250
  • 15
  • 31
  • What exactly is meant by the "processor" in this context? A logical core? Of a physical chip? Is there a difference between `Sleep(0)` and `Yield` on a multi-core single-processor system? – AnT stands with Russia Jun 21 '20 at 00:24
  • Link is dead, but still available via the Internet Archive. Suggesting an edit to this link to update it to https://web.archive.org/web/20161208024416/http://blog.coverity.com/2014/02/12/how-does-locking-work/ – Toadfish Oct 07 '20 at 20:06
5

According to the MSDN,

When using Sleep(0) The thread will not be scheduled for execution by the operating system for the amount of time specified.

With using Yield() The rest of the thread's current time slice is yielded. The operating system schedules the calling thread for another time slice, according to its priority and the status of other threads that are available to run.

So there's a minor difference in that. Thread.sleep will put a Thread into SLEEP mode with a recommendation that it stay there for the given number of milliseconds Thread.yield will put it into WAIT mode so it may run again straight away, or a higher process thread might step in.

Rhapsody
  • 6,017
  • 2
  • 31
  • 49
  • 3
    I think there's not a SLEEP state either at the CLR or the OS level, only WAITING and READY states. I think a Yield could move directly to READY, while a Sleep(n>0) would move to WAITING and once the interval is gone, would move it to READY. I think Sleep(0) could directly move to WAITING, without going through the SLEEP state. – Xose Lluis Jun 02 '10 at 14:57
  • 2
    @XoseLluis, even if `Yield()` is functionally equivalent to `Sleep(0)`, I'd prefer to use `Yield()`, because it states clearly the intention of the programmer. – Gerardo Lima Jul 18 '13 at 12:56
  • @GerardoLima -- they're not functionally equivalent if you're concerned about CPU usage. `Yield()` seems to use 4 times more CPU percentage than `Sleep(0)`. – rory.ap Jan 10 '18 at 14:18
4

Thread.Sleep(0) relinquishes the thread’s current time slice immediately, voluntarily handing over the CPU to other threads.

Framework 4.0’s new Thread.Yield() method does the same thing — except that it relinquishes only to threads running on the same processor.

source

Andrea
  • 11,801
  • 17
  • 65
  • 72
bussa
  • 195
  • 2
  • 8