Significance of Sleep(0)

前端 未结 8 2010
醉酒成梦
醉酒成梦 2020-12-03 02:19

I used to see Sleep(0) in some part of my code where some infinite/long while loops are available. I was informed that it would make the time-slice

相关标签:
8条回答
  • 2020-12-03 03:15

    Yes, it gives other threads the chance to run.

    A value of zero causes the thread to relinquish the remainder of its time slice to any other thread that is ready to run. If there are no other threads ready to run, the function returns immediately, and the thread continues execution.

    Source

    0 讨论(0)
  • 2020-12-03 03:15

    In one app....the main thread looked for things to do, then launched the "work" via a new thread. In this case, you should call sched_yield() (or sleep(0)) in the main thread, so, that you do not make the "looking" for work, more important then the "work". I prefer sleep(0), but sometimes this is excessive (because you are sleeping a fraction of a second).

    0 讨论(0)
提交回复
热议问题