33

I already found out with another question that Windows/MingW doesn't provide the nanosleep() and setitimer() alternatives to the obsolete usleep(). But my goal is to fix all warnings that cppcheck gives me, including the usleep() style warnings.

So, is there a workaround to somehow avoid usleep() on Windows without using cygwin or installing loads of new dependencies/libraries? Thanks.

Kara
  • 5,650
  • 15
  • 48
  • 55
blubberbernd
  • 3,291
  • 7
  • 30
  • 44
  • 2
    What are you using `usleep` for? – ta.speot.is Apr 27 '11 at 09:37
  • 1
    related question: http://stackoverflow.com/questions/85122/sleep-less-than-one-millisecond (I thought of proposing to use select as alternative) – stefaanv Apr 27 '11 at 09:49
  • Using select is a great idea. It is not terribly efficient, but then again when one is waiting, who cares. On the other hand, it is as portable as you can get. There exists no serious platform that doesn't have select. – Damon Apr 27 '11 at 10:41
  • 6
    just blindly trying to resolve all warnings issued by cppcheck, without reflecting on their rationale, and what that means for your project, does not seem to be the best idea – PlasmaHH Apr 19 '12 at 16:22
  • 1
    There's nothing obsolete about usleep. – Glenn Maynard Feb 17 '16 at 21:52

7 Answers7

54

I used this code from (originally from here):

#include <windows.h>

void usleep(__int64 usec) 
{ 
    HANDLE timer; 
    LARGE_INTEGER ft; 

    ft.QuadPart = -(10*usec); // Convert to 100 nanosecond interval, negative value indicates relative time

    timer = CreateWaitableTimer(NULL, TRUE, NULL); 
    SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0); 
    WaitForSingleObject(timer, INFINITE); 
    CloseHandle(timer); 
}

Note that SetWaitableTimer() uses "100 nanosecond intervals ... Positive values indicate absolute time. ... Negative values indicate relative time." and that "The actual timer accuracy depends on the capability of your hardware."

If you have a C++11 compiler then you can use this portable version:

#include <chrono>
#include <thread>
...
std::this_thread::sleep_for(std::chrono::microseconds(usec));

Kudos to Howard Hinnant who designed the amazing <chrono> library (and whose answer below deserves more love.)

If you don't have C++11, but you have boost, then you can do this instead:

#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
...
boost::this_thread::sleep(boost::posix_time::microseconds(usec));
Adi Shavit
  • 15,030
  • 2
  • 55
  • 121
  • 1
    And if you have C++14 you can use the `ms` user defined literal, as in `std::this_thread::sleep_for(100ms);`. This only works on constant values (obviously), and requires a `using` directive. See [this answer](http://stackoverflow.com/a/11276503/1291990) for details. – Michael Dorst Jan 07 '15 at 17:34
  • GCC implementation of `std::this_thread::sleep_for` will use `::nanosleep` when supported, but `::Sleep` on Windows with millisecond resolution. Don't know about VS one. – Mikhail Cheshkov Dec 08 '15 at 13:58
18

New answer for an old question:

Rationale for the new answer: Tools / OSs have been updated such that there is a better choice now than there was when the question was originally asked.

The C++11 <chrono> and <thread> std headers have been in the VS toolset for several years now. Using these headers this is best coded in C++11 as:

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

I'm using microseconds only as an example duration. You can use whatever duration happens to be convenient:

std::this_thread::sleep_for(std::chrono::minutes(2));

With C++14 and some using directives, this can be written a little bit more compactly:

using namespace std::literals;
std::this_thread::sleep_for(2min);

or:

std::this_thread::sleep_for(123us);

This definitely works on VS-2013 (modulo the chrono-literals). I'm unsure about earlier versions of VS.

Howard Hinnant
  • 179,402
  • 46
  • 391
  • 527
  • 3
    Just the other day, I ran into a Windows-only deadlock in a trivial test program that used sleep_for to wait for a few microseconds. MS's implementation seems to boil down to Sleep (or the equivalent), which takes milliseconds. Calling sleep_for for less than a millisecond translates to Sleep(0). The Sleep() documentation explains that Sleep(0) _can_ cause a deadlock when called from a threadpool thread under certain circumstances, and those circumstances were common in our test suite. Calling sleep_for with milliseconds resolved the problem. – Adrian McCarthy Aug 05 '19 at 15:28
  • 1
    That sounds like it is worthy of a bug report. The implementation should round finer precisions **up** to the next sleep-able precision, not down. – Howard Hinnant Aug 05 '19 at 15:40
9

The millisecond regime of the Sleep() function is well described and well understood. It does not do anything unpredictable. Sometimes the function is blamed to perform unpredictable, i.e. returning before the delay has expired. I need to say that this is wrong. Careful investigation will confirm that its behaviour is absolutely predictable. The only problem is that there is plenty to read about it and most of it is kiddish. It is also often said that windows it not a real-time OS. But such comments don't contribute anything, moreover such comments are used to hide the lack of knowledge. It makes me sort of angry, that not even microsoft notices this and provides better documentation.

However, without exaggerating this little answer: The sleep() function is precise, when used in a proper way and when knowing its characteristics. Particular attention has to be given to sleep(0). This is a very powerfull tool, particulary when used together with process priority class, thread priority, multimedia timer settings, and processor affinity mask.

So generally a true sleep can be performed easely and safe down to the systems interrupt period. When it comes to sleeps shorter than the interrupt period spinning is required. A higher resolution time source has to be used in oder to spin for shorter periods in time. The most common source for this is the performance counter. QueryPerformanceCounter(*arg) delivers an incrementing *arg. QueryPerformanceFrequency(*arg) delivers the frequency at which the performance counter increments. This is typically in the MHz regime and varies, depending on the underlying hardware. A frequency in the MHz range provides microsecond resolution. This way something of high resolution can be used to wait for a desired time span to expire. However, the accuracy of this has to be looked at carefully: The OS returns the performance counter frequency as a constant. This is wrong! Since the frequency is generated be a physical device, there is always an offset and it also not a constant. It has thermal drift. More modern systems do have less drift. But if the thermal drift is just 1ppm, the error will be 1us/s. The offset can easely be several 100. An offset of 100 in 1MHz corresponds to 100us/s.

If a thread shall wait for any time at high resolution, it shall establish a service thread. Both thread shall share a named event. The service thread shall sleep until 1 interrupt period ahead of the desired sleep delay and then spin on the performance counter for the remaining microsecond. When the service thread reaches the final time, it set the named event and ends. The calling thread will wake up, because it was waiting for the named event by means of a wait function.

Summary:

  • Sleep is well understood but poorly documented.
  • A service thread can mimic sleeps at high resolution.
  • Such a service thread coulb be esablished as a system wide service.
  • Accuracy of the performance counter is to be looked at carefully. A calibration is required.

More detailed information can be found at the Windows Timestamp Project

Arno
  • 4,664
  • 3
  • 31
  • 56
  • Not quite! The resolution is not necessarily the inverse of the reported frequency — On one of my boxes, QPF() returns the nominal CPU clock frequency but QPC() always returns a multiple of 10. I'm guessing this is due to the implementation of an "invariant TSC" a.k.a. `constant_tsc`. – tc. Oct 08 '12 at 13:42
  • @tc.: Did I suggest to not trust the result of QPF? Yes I did, because its result never shows the truth, it never shows the physical frequency. It shall be considered as an estimated `constant`. Reporting the CPU clock frequency in 1Hz units is somewhat stupid. Even units of 10 are ridiculous because the real frequency deviates by several ppm. 1ppm equals 2000Hz on a 2GHz hardware. So a more realistic granularity for the output of QPF should/could be 1ppm of the frequency. BTW: This would give people also the hint that the output of **QPF is only an estimate**. – Arno Oct 08 '12 at 16:30
  • I was specifically responding to "A frequency in the MHz range provides microsecond resolution" and I think you misinterpreted me: My results suggest that QPC() counts up steps of 10 on one of my boxes, giving a *resolution* of about 1/(240 MHz); not 1/(2.4 GHz). Clock drift/skew is another can of worms entirely... – tc. Oct 08 '12 at 17:36
  • @tc.: Well, 1/240MHz is ~ 4ns. That's far better than microsecond resolution as well. And: Of course the resolution is also determined by the quantisation/granularity (presumed in all of the above). – Arno Oct 09 '12 at 06:36
  • @tc: ... was just brought to my attention: `constant TSC` would return QPF() with CPU freq./1024. The CPU freq. does not exist by hardware, it is gernerated by a frequency multiplier (1024). – Arno Aug 14 '13 at 06:58
  • Arno, your Timestamp Project site provides incredible in-depth research to all things time on Windows, up to Windows 8.1. Chapeau! – Evgeniy Berezovsky Jun 08 '15 at 23:24
5

It depends what granularity you need. If you are talking milliseconds, then the Win32 Sleep function will do the job - see http://msdn.microsoft.com/en-us/library/ms686298%28v=vs.85%29.aspx. If you are talking microseconds, then there is no easy way to do it, and you would be lucky to get that sort of timer resolution on Windows (which is not an RTOS), or on Linux, come to that.

4

I found this blog post about it. It uses QueryPerformanceCounter. The function posted:

#include <windows.h>

void uSleep(int waitTime) {
    __int64 time1 = 0, time2 = 0, freq = 0;

    QueryPerformanceCounter((LARGE_INTEGER *) &time1);
    QueryPerformanceFrequency((LARGE_INTEGER *)&freq);

    do {
        QueryPerformanceCounter((LARGE_INTEGER *) &time2);
    } while((time2-time1) < waitTime);
}

I hope this helps a bit.

genpfault
  • 47,669
  • 9
  • 68
  • 119
orlp
  • 98,226
  • 29
  • 187
  • 285
  • 8
    This is a busy wait. It will waste CPU cycles. –  Jul 13 '12 at 17:08
  • 6
    @tibor Yes it will. As far as I know the windows process scheduler does not provide any sleep procedure with microsecond precision. You can create a hybrid sleep function though, use the default Sleep function for the milliseconds and use busy waiting for the last small wait time. This can reduce the performance hit to less than 5 ms of busy waiting without sacrificing precision. – orlp Jul 16 '12 at 07:47
  • 4
    The current code is ignoring the value of `freq`. Instead, if waitTime is given in microseconds, the final test should be `time2-time1 < waitTime*freq/1000000` . – Hugues Apr 26 '14 at 15:26
1

I'm quite late to the party but I want just to add something to this question. If you want to achieve portability using microseconds resolution than use select() system call using empty file descriptor set. It will work both on linux and on windows, i.e. it can be called using an uniform interface (the behavior can be still different especially on Windows where you can ask for 1 microsecond but you get 1ms sleep). If you want to use a third-party library use Boost, but a recent version. The time related std api are just a mess and I provide a summary here:

  1. Windows: sleep_for, sleep_until, condition_variable, future and so on wait_for/wait_until methods are totally unreliable because they are based on system_clock, so if the time changes your application could hang. They fixed this problem internally but since it's an ABI break, they didn't release it yet, not available even on latest VS 2019.
  2. Linux: sleep_for, sleep_until are ok, but condition_variable/future wait_for/wait_until are unreliable because they are based too on system clock. It's fixed with Gcc 10 and GlibC 2.6 bug link.
  3. Boost: Same as above but they fixed time problems with version 1.67.

So if you want portable code make your own solution or just use Boost 1.67+, don't trust standard implementation c++11 chrono because the implementation performed is really bad.

greywolf82
  • 20,269
  • 17
  • 45
  • 90
-2

usleep() works with microseconds. In windows for getting microsecond precesion you should use QueryPerformanceCounter() winapi function. Here you can find how get that precesion using it.

Mihran Hovsepyan
  • 10,110
  • 13
  • 57
  • 108
  • 5
    While QueryPerformanceCounter can give high-resolution clock values, it is not a real-time sleeping API -- it can only allow you to busy-wait. – Jon Watte Sep 09 '14 at 00:25
  • 2
    Nothing in Windows is "real time." For values in microsecond or nanosecond range, sleeping isn't a concept. The Windows scheduler uses quanta on the order of 10s of milliseconds. Unless you're doing it constantly, a busy wait on the order of microseconds isn't horrible. – Adrian McCarthy Aug 05 '19 at 15:41