Is Thread.Sleep(Timeout.Infinite); more efficient than while(true){}?

泪湿孤枕 提交于 2019-11-27 13:33:56

问题


I have a console application that I would like to keep open all of the time while still listening in to events. I have tested Thread.Sleep(Timeout.Infinite); and while (true) { } and both allow the events to be raised while keeping the console application open. Is there one that I should be using over the other? If the thread is sleeping, is there anything that I should not be doing, such as modifying a static collection declared in the scope of the class?


回答1:


I would recommend using a ManualResetEvent (or other WaitHandle), and calling ManualResetEvent.WaitOne.

This will have a similar effect to sleeping forever, except that it provides you a clean way to exit from your infinite "block" when desired (by calling Set() on the event).

Using while(true) will consume CPU cycles, so it's definitely something to avoid.

is there anything that I should not be doing, such as modifying a static collection declared in the scope of the class?

In general, no. Since your thread will be blocked, there shouldn't be any synchronization issues with using shared data (provided the items within the collection don't have specific requirements, such as user interface elements which must be used on a thread with a proper synchronization context.)




回答2:


Unlike while(true)..., Thread.Sleep does not use CPU cycles, so in this sense, the sleep is more efficient. In general, using Busy Waiting outside of spinlocks is strongly discouraged.

If the thread is sleeping, is there anything that I should not be doing?

Since your thread is blocked upon entry to Thread.Sleep, anything that you wish to do to its resources is a fair game.




回答3:


I think the call

while (true) { ... } 

is computationally intensive, since the thread never stops, wheareas

Thread.Sleep(Timeout.Infinite);

actually gets the thread to sleep with help of OS native schedulers. And then the thread actually stops, so I suppose it's less computationally demanding.




回答4:


Yes, while(true) consumes CPU while sleep() works in a smarter way: The sleep() function puts the current execution context to sleep; it does this by calling a syscall to invoke the kernel sleep function which atomically
(a) sets a wake-up timer
(b) marks the current process as sleeping
(c) waits until the wakeup-timer fires or an interrupt occurs

If you call sleep(), the CPU can do other work.

That's one reason why sleep() is useful.

A useful link - Be careful when using Sleep




回答5:


Calling the Thread.Sleep method causes the current thread to immediately block for the number of milliseconds or the time interval you pass to the method, and yields the remainder of its time slice to another thread.

https://msdn.microsoft.com/en-us/library/tttdef8x(v=vs.110).aspx




回答6:


Since C# 7.1 you can make Main method asynchronous. That means instead of using busy-wait or thread-locking sleeping, you can suspend Main method asynchronously as a Task. This way thread which runned Main will not be locked. And with Cts.Cancel(); you can easily release main-task to exit application.

readonly CancellationTokenSource Cts = new CancellationTokenSource();
static async Task Main(string[] args)
{
    /* your code here */

    await Task.Delay(Timeout.Infinite, Cts.Token);
}


来源:https://stackoverflow.com/questions/13406435/is-thread-sleeptimeout-infinite-more-efficient-than-whiletrue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!