I'm writing a C++ single-threaded Unix daemon that listens for data arriving at a port. The whole application logic is contained into an infinity loop. The problem is that the daemon consumes a lot of CPU cycles, specifically because the loop is being continuously executed. I’ve looking around for a good solution to this problem, but I haven’t found any clue on how to handle this situation. I suppose that a good approach could be to sleep the loop for a few milliseconds and let the system to use some CPU cycles before going to the next loop step.
The question is: Do anybody know if this approach is correct? And, if is it correct, do anybody know how many seconds should the application sleep?
Any help will be appreciated.
Thanks in advance,