C Main Loop without 100% cpu

前端 未结 11 2327
半阙折子戏
半阙折子戏 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:01

    What exactly are you checking for?

    If you're checking something volatile that is changed by hardware or another process, just call sleep in your loop.

    If you are waiting on a file descriptor or a network socket descriptor, you will want to use select or poll in your loop to wait for the descriptor to have data ready for consumption.

提交回复
热议问题