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) < retTime); // Loop until it arrives.
}
sleep
? \$\endgroup\$