tags:

views:

408

answers:

4

Hello, I need a function like Sleep(time); that pauses the program for X milliseconds, but in C++.

Please write which header to add and the function's signature. Thank you!

+1  A: 
#include "windows.h" 
sleep(10);

for unix probably #include <unistd.h>

just google it...

Dani
On Windows, Sleep() is capitalized. On Unix, there is usleep() in unistd.h
asveikau
+4  A: 

There is no portable way to do this.

A portable way is to use Boost or Ace library. There is ACE_OS::sleep(); in ACE.

AlexKR
... and the (supposed) reason there is no portable way to do it in the standard is because the a clock's precision (smallest unit of time) is hardware-dependent or OS-dependent. No, I don't find it a convincing reason either, but there we are.
wilhelmtell
There is no such thing as thread defined by standard... and you want sleep. Sleep is a OS provided functionality. I can have environment which does not provide me such feature.
AlexKR
@wilhelmtell: That is not the reason at all. Who is it making this supposition other than yourself? There is no standard for thread support (yest), and if there are no threads (or rather only one thread), there is no need for a thread sleep rather than a simple 'busy-wait', which can be implemented with <time.h>/<ctime>. The support must be provided by the thread library or OS.
Clifford
+1  A: 

On unix, include #include <unistd.h>... The call your interested in is usleep()... Which takes microseconds, so you should multiply your millisecond value by 1000 and pass the result to usleep()...

dicroce
+5  A: 

On Windows/Unix:

#ifdef _WIN32
    #include <windows.h>
    inline void sleep(unsigned pMilliseconds)
    {
        ::Sleep(pMilliseconds);
    }
#else
    #include <unistd.h>
    inline void sleep(unsigned pMilliseconds)
    {
        static const unsigned MilliToMicro = 1000;
        ::usleep(pmilliseconds * MilliToMicro);
    }
#endif
GMan