Simple C app using 50% cpu

后端 未结 6 1667
不思量自难忘°
不思量自难忘° 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:55

    If you want your code to sleep for 4 seconds then you can use sleep(4) to do that and it will almost certainly not consume CPU like your wait() function does. Note that sleep(4) will block execution of the remainder of your single-threaded program and if you do not want that then you will need something more sophisticated, but I suspect sleep(4) will suffice here.

    Also, your code will eventually exhaust the stack because printwow() calls timer_func() which calls printwow() which calls timer_func() etc. etc. ad infinitum in a recursive loop. You need to fix this, probably by using a for/while loop rather than recursion.

提交回复
热议问题