Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread.Sleep or Thread.Yield

I have a method that uses a background worker to poll a DLL for a status looking something like this:

var timeout = DateTime.Now.AddSeconds(3); while (System.Status != Status.Complete  // our status is not complete        && DateTime.Now < timeout         // have not timed out        && !_Worker.CancellationPending)  // backgroundworker has not been canceled {     //Thread.Yield();     //Thread.SpinWait(1);     //Thread.Sleep(1); } 

When looking at my CPU %, yield() and spinwait() cause my app to shoot up to 50% on my PC. With Sleep(1) my CPU % stays down at 6%. I have been told that that I should choose Thread.Yield(), however the spikes in CPU % bother me. What is best practice for something like this?

like image 808
poco Avatar asked Jul 14 '12 03:07

poco


People also ask

Does sleep yield the thread?

Sleep() causes the currently executing thread to sleep (temporarily cease execution). Yield() causes the currently executing thread object to temporarily pause and allow other threads to execute.

What is a thread yield?

In computer science, yield is an action that occurs in a computer program during multithreading, of forcing a processor to relinquish control of the current running thread, and sending it to the end of the running queue, of the same scheduling priority.

What is wait () sleep () yield ()?

The main difference between wait and sleep is that wait() method releases the acquired monitor when the thread is waiting while Thread. sleep() method keeps the lock or monitor even if the thread is waiting.


1 Answers

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.

like image 52
Eric J. Avatar answered Oct 04 '22 01:10

Eric J.