0
votes

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
Neither of these is a standard library function. Which library are you referring to? - Don Reba
I think normally sleep() sets the processor free during sleep time, while delay() needs CPU-usage, but I'm not quite sure about it. - Klaus
Dos.h is not a part of the standard library. And both functions guarantee only that approximate delay will be made. All other side effects are implementation defined. - Ternvein
If you didn't understand the answer I gave to your OTHER question about this, how about asking there in the comments? - user2371524
Well like this ... suddenly mentioning dos.h implies this question is about MS DOS. Something nobody would expect nowadays. @RohanGayen you should make these things explicit and add proper tags ... neither delay() nor sleep() are standard C functions. - user2371524

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
0
votes

Sleep relinquishes the CPU for specified amount , so that other process can execute. While Delay is just busy waiting for the amount to expire and then continue once expires, it do not execute any other process during this period.