C Main Loop without 100% cpu

前端 未结 11 2356
半阙折子戏
半阙折子戏 2020-12-14 08:53
#include 

int main() {
  while(!DONE) {
    /* check for stuff */
  }
  return 0;
}

The above code sample uses 100% cpu until DONE

相关标签:
11条回答
  • 2020-12-14 09:19

    Sleep(0); would be enough i guess

    0 讨论(0)
  • 2020-12-14 09:20

    use

    Sleep(int milliSeconds)

    0 讨论(0)
  • 2020-12-14 09:21

    It depends what you want to do inside this loop.

    If you are waiting inside the loop (i.e. if keypressed { do something} then your mechanism will waste system resources giving nothing in return. A faster processor will just make more idle loops. This can be solved by waiting for events Not just sleep, but preferably an event which triggers that something meaningful can be done. For instance, a file operation (stdin is also a file) would be a portable mechanism. This will give way to other applications until data is available. When you become more specific it may be required to dive into semaphores or signals which are often OS dependent. An abstraction layer can resolve this.

    If you are doing something useful (i.e. processing a lot of data), then 100% cpu load just means that the processor is used in the most efficient way. You can rely on the operating system to give way to other and possibly higher priority tasks.

    Using a function like sleep will lower cpu usage, but your application will be slower. It will require to get a tradeoff between acceptable performance and cpu load. The maximum execution speed will be defined by your sleep parameter, and no longer by the cpu speed. Also, if power is a concern (i.e. battery life time), then this will require the cpu to wakeup (end of sleep period) with no work to be done; i.e. a different waste of system resources.

    0 讨论(0)
  • 2020-12-14 09:22

    If I'm guessing correctly (I don't know about it), the equivalent of App.ProcessMessages is doing blocking IO. And as I don't know of any implementation of C on multitasking OS which uses polling, any standard C IO should be safe.

    0 讨论(0)
  • 2020-12-14 09:26

    Your two options would be polling, and some kind of event notification.

    Polling would be easiest to program -- basically you have your thread sleep for a short while every pass through the loop. This releases the processor to other tasks. The downside is that there will be a delay in your "check for stuff" code -- so if you're sleeping for a second, it might be up to a second before your code detects the condition. Your code here will be easy to port.

    The other option is to wait on a POSIX conditional, or Windows event or something like that. Not only will this code be changed, but then the "stuff you're checking" will need to trigger the flag to say it is done. This would be a little less portable code, although there are probably libraries to abstract away the platform. But you'll get immediate results to the event, and no wasted processor time checking for things that aren't there.

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