Delaying a function call for 30 seconds using sleep() or checking the time in a loop I need a way to delay a function call by 30 seconds on a machine that runs on electric power, not battery (if it matters). I have heard from my colleague not to use sleep, but this post suggests it:


sleep(30) // sleep for 30 sec



Reasons invoked for not using sleep include:


I will violate Single Responsibility Principle.
Abstraction leak, etc.


Not sure why though; I feel it's good because it reduces CPU load and executes important tasks. In the below approach I am trying to just avoid sleep because I am asked not to use it though I am curious to know what a better (best?) approach would be?

void waiting (unsigned int secs)
{
        unsigned int retTime = time(0) + secs;   // Get finishing time.
        while (time(0) &lt; retTime);               // Loop until it arrives.
}
There is no reason to avoid doing sleep(30);. Your colleague's concerns about the single responsibility principle and abstraction leak are unfounded. sleep() is designed to do exactly what you want, so why not use it? Constantly checking the time in a tight loop is a giant waste of computing power for no advantage at all.