Simple C app using 50% cpu

后端 未结 6 1720
不思量自难忘°
不思量自难忘° 2021-01-25 03:31

I have a simple C app that uses constant 50%. I don\'t know why but I like to minimize it as much as possible.

#include 
#include          


        
6条回答
  •  南方客
    南方客 (楼主)
    2021-01-25 03:57

    Your loop:

    while (clock() < endwait) {}
    

    is busy-waiting. You are basically having the following conversation with the CPU.

    "Are we there yet?" "No."

    "Are we there yet?" "No."

    "Are we there yet?" "No."

    (repeat several gazillion times)

    "Are we there yet?" "Yes."

    You are better off using a function like sleep() which tells the CPU to tell you when it's ready.

提交回复
热议问题