CPP WINDOWS: is there a sleep function in microseconds?

I know there are milliseconds (Sleep (milli))

but I could not find it for micro ...

+4
source share
5 answers

You can use the rdtsc or QueryPerformanceCounter Windows API function to get high-resolution counters. You can calibrate them, then GetTickCount , or, for example, functions of time.

+1
source

The VS 11 dev preview includes part of the standard thread library. So now you can say:

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

Of course, this does not mean that the stream wakes up after such an amount of time, but it should be as close to the platform (and the library implementation) as possible. As other comments note, Windows does not actually allow threads to sleep for extended periods.

+1
source

Why do you need to sleep with your thread for less than 1 millisecond?

OS planner cannot organize this. Even if you sleep your thread in 1 millisecond, it can sleep for 1 to 15 ms, since the scheduler will not โ€œhave timeโ€ to wake it. Although this may be related to the environment, you definitely cannot achieve more accurate accuracy than 1 ms on Windows.

Check What is the Impact of Thread.Sleep (1) on C #?

0
source

Windows cannot sleep in less than a millisecond. Time slices, as a rule, far exceed 1 ms, so this is actually not possible, even if the stream has the highest priority.

If you don't care about burning the processor, you can spin until your QueryPerformanceCounter runs out of time.

0
source

I just wrote a detailed comment about the sleep () function and rotated the performance counter. In order not to enter it here again, here is the link:

C ++, usleep () is deprecated, workarounds for Windows / MingW?

0
source

Source: https://habr.com/ru/post/1394435/


All Articles