Both delay() and sleep() function suspends the system for some amount of time, delay takes milisecond as argument while sleep takes second as argument. Besides this, is there any differences between these two functions? And among them, which gives more accurate result?
2 Answers
2
votes
They do the same thing except one sleeps for number of seconds while the other sleeps for milliseconds.
You should go with Reference to std::this_thread::sleep_for:
std::this_thread::sleep_for
instead in c++ if you can. windows.h have Sleep and unix have usleep.
This is another implementation found online that might better fit your needs:
#if defined(__WIN32__) || defined(_WIN32) || defined(WIN32) || defined(__WINDOWS__) || defined(__TOS_WIN__)
#include <windows.h>
inline void delay( unsigned long ms )
{
Sleep( ms );
}
#else /* presume POSIX */
#include <unistd.h>
inline void delay( unsigned long ms )
{
usleep( ms * 1000 );
}
#endif
sleep()sets the processor free during sleep time, whiledelay()needs CPU-usage, but I'm not quite sure about it. - Klausdos.himplies this question is aboutMS DOS. Something nobody would expect nowadays. @RohanGayen you should make these things explicit and add proper tags ... neitherdelay()norsleep()are standard C functions. - user2371524