Cross-platform Sleep function for C ++

Can macros make cross-platform sleep code? for example

#ifdef LINUX #include <header_for_linux_sleep_function.h> #endif #ifdef WINDOWS #include <header_for_windows_sleep_function.h> #endif ... Sleep(miliseconds); ... 
+30
c ++ cross-platform sleep
Jun 06 '12 at 16:23
source share
7 answers

Yes there is. What you do is transfer the various sleep system calls to your own function, as well as include statements, as shown below:

 #ifdef LINUX #include <unistd.h> #endif #ifdef WINDOWS #include <windows.h> #endif void mySleep(int sleepMs) { #ifdef LINUX usleep(sleepMs * 1000); // usleep takes sleep time in us (1 millionth of a second) #endif #ifdef WINDOWS Sleep(sleepMs); #endif } 

Your code then calls mySleep for sleep, not for direct system calls.

+36
Jun 06 '12 at 16:38
source share

Yeah. But this only works in C ++ 11 and later .

 #include <chrono> #include <thread> ... std::this_thread::sleep_for(std::chrono::milliseconds(ms)); 

where ms is the amount of time you want to sleep in milliseconds.

You can also replace milliseconds with nanoseconds , microseconds , seconds , minutes or hours . (These are specializations of the type std :: chrono :: duration .)

Update: In C ++ 14 , if you sleep for a certain amount of time, for an instance of 100 milliseconds, std::chrono::milliseconds(100) can be written as 100ms . This is due to user literals that were introduced in C ++ 11 . In C ++ 14, the chrono library chrono been expanded to include the following user literals:

Effectively, this means that you can write something like this.

 #include <chrono> #include <thread> using namespace std::literals::chrono_literals; std::this_thread::sleep_for(100ms); 

Note that while using namespace std::literals::chrono_literals provides the least amount of namespace contamination , these operators are also available when using namespace std::literals or using namespace std::chrono .

+45
Jun 30 '12 at 18:18
source share

shf301 had a good idea, but this method is better:

 #ifdef _WINDOWS #include <windows.h> #else #include <unistd.h> #define Sleep(x) usleep((x)*1000) #endif 

Then use like this:

 Sleep(how_many_milliseconds); 
+19
Apr 7 '13 at
source share

Get Boost .

 #include <boost/thread/thread.hpp> #include <boost/date_time/posix_time/posix_time.hpp> ... boost::this_thread::sleep(boost::posix_time::millisec(milliseconds)); 
+18
Jun 06 '12 at 16:33
source share

The original solution is to call select () (requires Winsock). This particular call has exactly the same behavior on Linux and Windows.

 long value; /* time in microseconds */ struct timeval tv; tv.tv_sec = value / 1000000; tv.tv_usec = value % 1000000; select(0, NULL, NULL, NULL, &tf); 
+8
Jun 06 '12 at 16:41
source share

On linux, remember that usleep has a limit. You cannot sleep for more than 1000 seconds.

I would write like this:

 struct timespec req={0},rem={0}; req.tv_sec=(milisec/1000); req.tv_nsec=(milisec - req.tv_sec*1000)*1000000; nanosleep(&req,&rem); 
+1
Jun 07 '12 at 8:50
source share

Just do

 #include <ctime> int Sleep(int ms) { clock_t starting = clock(); while( clock() - starting < ms ) {} return 0; } 

This checks the current time (in milliseconds), subtracts from it the start time, which gives the time used by the function. If this number is less than the critical value (target time length), then it does nothing, killing a small amount of time. This is repeated until the time exceeds a critical value, and then stops.

EDIT: fixed code errors

-2
Jan 27 '17 at 23:06
source share



All Articles