CPU friendly infinite loop

前端 未结 11 1935
心在旅途
心在旅途 2020-12-04 09:15

Writing an infinite loop is simple:

while(true){
    //add whatever break condition here
}

But this will trash the CPU performance. This ex

11条回答
  •  情书的邮戳
    2020-12-04 09:47

    The Timer approach is probably your best bet, but since you mention Thread.Sleep there is an interesting Thread.SpinWait or SpinWait struct alternative for similar problems that can sometimes be better than short Thread.Sleep invocations.

    Also see this question: What's the purpose of Thread.SpinWait method?

提交回复
热议问题