Advertisement

sleep c++

Started by June 05, 2017 02:38 AM
21 comments, last by Ryan_001 7 years, 5 months ago

for windows -
#include <windows.h>
VOID WINAPI Sleep(DWORD Milliseconds);


for linux 
# include <unistd.h>
unsigned int sleep(unsigned int Seconds);

Notice though that they use different arguments. In windows its in milliseconds, and in linux its in full seconds

In general, on windows, if you just need to keep the thread from blocking other processes, use Sleep(0); which will immediately return control to the calling thread if no other threads or processes are waiting. Any value higher than 0 will guarantee that control does not return to the calling thread PRIOR to that time elapsing, but there is no guarantee of it happening exactly at that time. So e.g. Sleep (1000); would not return before 1000 milliseconds had elapsed, but it could very well not return for 5 seconds, or 5 days. If you want to actually wait a specified time, e.g. being laptop battery friendly, you should instead use timer functions such as

UINT_PTR WINAPI SetTimer(...);

https://msdn.microsoft.com/en-us/library/windows/desktop/ms644906(v=vs.85).aspx

These can have better resolution, and callback timers in specific are virtually guaranteed to execute at the specified interval except on the most heavily overloaded system.

POSIX has usleep which can sleep for microseconds, although if the sleep is very short its probably just a pause loop.

If you are using a non-decade-old compiler you'll also have access to:

std::this_thread::sleep_for(std::chrono::microseconds(usec));

in C++, which will be portable.. if that means anything to you

Advertisement

If you want to use Sleep(), you need to include WinBase.h.

actually I used windows.h and it works just fine

1 hour ago, phil67rpg said:

actually I used windows.h and it works just fine

For future reference, when you're asking a question like this, it's best to tell us what you're trying to do. You'll get much better answers that way.

is there a difference between how sleep() and this_thread::sleep_for(chrono::microseconds(1000)) works?

Advertisement
12 hours ago, phil67rpg said:

is there a difference between how sleep() and this_thread::sleep_for(chrono::microseconds(1000)) works?

In theory, they can share the same low-level sleeping mechanism and behave in exactly the same way, they could be completely unrelated, or they could be similar except for some details or special cases that might or might not be relevant for you.

In practice, try both and measure actual sleeping time accuracy, on every platform you care about: sleeping is important enough to deserve some testing effort.

Omae Wa Mou Shindeiru

14 hours ago, phil67rpg said:

is there a difference between how sleep() and this_thread::sleep_for(chrono::microseconds(1000)) works?

https://msdn.microsoft.com/en-us/library/hh874757.aspx says: 

Quote

The implementation of steady_clock has changed to meet the C++ Standard requirements for steadiness and monotonicity. steady_clock is now based on QueryPerformanceCounter() and high_resolution_clock is now a typedef for steady_clock. As a result, in Visual C++ steady_clock::time_point is now a typedef for chrono::time_point<steady_clock>; however, this is not necessarily the case for other implementations.

Which is good because QPF is the best timer on a Windows system, is quite stable and has nanosecond precision.  If you're curious as to the details this link here: https://msdn.microsoft.com/en-us/library/windows/desktop/dn553408(v=vs.85).aspx.  How  exactly sleep_for() and sleep_until() are implemented, I'm not sure.

Sleep() we do know uses the internal system clock interrupt, which fires every few milliseconds (7-15ms from what I understand on most systems).

Without some benchmarking I can't say for sure, but I'm certain that sleep_for() and sleep_until() will be at least as accurate as Sleep() and possibly better.

So I threw together a little test benchmark.  On my system (Windows 7, Intel i7) both sleep_for() and Sleep() were identical.

When VS (and a few other programs in the background) were open I was getting 1ms accuracy for both (+/- 10us or so).  So both performed well.  With everything closed though that number jumped up and would vary between 5ms and 15ms; but whatever the number was for sleep_for(), Sleep() was identical.  So either VS or some other program in the background was calling timeBeingPeriod(1).  It seems either way, under the hood, sleep_for() is identical to Sleep() on Windows 7 with VS 2017.

I learnt about sleep(); in class too. Remember we just plug in the time in ms.

Need to use #include<time.h> as I remember correctly lol been a while.

This topic is closed to new replies.

Advertisement