Are Thread.Sleep(0) and Thread.Yield() Statements Equivalent

Are Thread.sleep(0) and Thread.yield() statements equivalent?

No. The most obvious difference is that sleep() throws the (checked) InterruptedException. In practice, the effect may be almost the same, but it's entirely implementation-dependant.

I'd wager that doing each a million times in a row would take much longer for sleep(), since system timer granularity probably often causes it to actually sleep for a non-negligible amount of time.

Thread.Sleep or Thread.Yield

Thread.Yield will interrupt the current thread to allow other threads to do work. However, if they do not have any work to do, your thread will soon be rescheduled and will continue to poll, thus 100% utilization of 1 core.

Causes the calling thread to yield execution to another thread that is ready to run on the current processor. The operating system selects the thread to yield to.

Thread.Sleep will schedule your thread to run again after the sleep time expires, thus much lower CPU utilization.

Blocks the current thread for the specified number of milliseconds.

Given the choice between the two, Thread.Sleep is better suited for your task. However, I agree with the comment from @Bryan that a Threading.Timer makes for a more elegant solution.

What is difference between sleep() method and yield() method of multi threading?

sleep() causes the thread to definitely stop executing for a given amount of time; if no other thread or process needs to be run, the CPU will be idle (and probably enter a power saving mode).

yield() basically means that the thread is not doing anything particularly important and if any other threads or processes need to be run, they should. Otherwise, the current thread will continue to run.

Thread.Sleep(0) vs Thread.Sleep(1) vs Thread.Sleep(2)

Thread.Sleep(n); // Where n is milliseconds

When N==0

This tells the system you want to forfeit the rest of the thread’s timeslice and let another waiting thread (whose priority >= currentThread) run (this means you won't be sure when you get your control back).

If there are no other threads of equal priority that are ready to run, execution of the current thread is not suspended.

When N>=1 (be it N=1 or N=2)

Will block the current thread for at least the number of timeslices (or thread quantums) that can occur within n milliseconds, In other words it will relinquish the remainder of its time slice to any other thread un-conditionally.


The Windows thread scheduler ensures that each thread (at least those with the same priority) will get a fair slice of CPU time to execute. The reason for blocking the current thread for atleast specified interval is because scheduler might take longer than the specified interval before it gets around to that thread again.

References: 1, 2, 3


Update

In my pursuit to find the working of Sleep in versions prior to C# 3, I've come across interesting articles (on and before year 2005) that I felt is worth an update.

In short, I did not find any difference with regards to threads relinquishing to higher or same priority when n=1 or n=2.

From Vaults: 1, 2, 3

what's the difference between yield() and sleep()?

yield merely says: now is a good time to let another thread run and is a hint to the scheduler. sleep really does that: sleep at least the given time.

Whether Thread.yield() will be useful for good Coding practice?

Thread.yield() is a relic from thirty or so years ago when threads were implemented on single-CPU computers with no support from the operating system, by a technique called "cooperative multitasking".

Back in those days, the only way a thread ever got to run is when some other thread "yielded" the CPU by calling some function in the thread library. Usually this would happen at points where one thread needed to wait for something, and so naturally, that was a good time to let other threads run.

The yield() call was for the special case where a thread was doing a long computation, and it didn't want to wait for anything. The programmer would sprinkle a few yield() calls at strategic places in the algorithm to make sure that other threads could respond to events in a timely fashion.

Let me say that again, because it's important: A thread would call yield() at points where it did not want to wait.

Some form of yield() has survived in just about every threading library since, but it no longer serves any purpose unless you are re-implementing the lowest-level synchronization primitives as a learning exercise.



Related Topics



Leave a reply



Submit