8

Possible Duplicate:
Sleep Less Than One Millisecond

How can I make a program sleep for a nanosecond? I searched the Internet, and I found several ways to sleep, but:
windows.h's Sleep() sleeps only for milliseconds.
ctime's nanosleep() is only for POSIX systems, and I'm using Windows.
I also tried this:

int usleep(long usec)
{
    struct timeval tv;
    tv.tv_sec = usec/1000000L;
    tv.tv_usec = usec%1000000L;
    return select(0, 0, 0, 0, &tv);
};

But Code::Blocks says:

obj\Release\main.o:main.cpp|| undefined reference to `select@20'|

I tried many things, but everything failed. What should I do?

Community
  • 1
  • 1
SmRndGuy
  • 1,719
  • 5
  • 30
  • 49
  • Maybe you should include winsock's header to call the select? – Gang YIN Jun 16 '12 at 13:55
  • 5
    Why do you want to sleep for such a short time? Does it has really some sense to you (think of cache faults, context switches and many other non-reproducible causes of longer than nanoseconds delays...) – Basile Starynkevitch Jun 16 '12 at 13:59
  • 1
    The time it takes to execute a single CPU instruction is on the order of nanoseconds. You might need dedicated hardware to achieve the timing you're looking for. – Emile Cormier Jun 16 '12 at 14:45
  • I'm currently trying to decrease CPU cycles when it enters a loop. Sleep is too slow for that so I was looking for a faster sleep – SmRndGuy Jun 16 '12 at 14:49
  • You might consider [alternatives](http://en.wikipedia.org/wiki/Busy_waiting#Busy-waiting_alternatives) to busy waiting. – Sam Miller Jun 16 '12 at 16:19
  • Can you describe the higher-level problem you're trying to solve? What events trigger the sleep, and what are you going to do when you wake up? – Neeraj Singh Jun 16 '12 at 19:57
  • add -lWSock32 to link winsock – Hassen Dhia Sep 17 '17 at 01:26

2 Answers2

17

Using C++11

#include <chrono>
#include <thread>
...
std::this_thread::sleep_for(std::chrono::nanoseconds(1));

Note that the implementation may sleep longer than the given period.

authchir
  • 1,605
  • 14
  • 26
Stephan Dollberg
  • 32,985
  • 16
  • 81
  • 107
  • 1
    sleep_for on Windows has flacky resolution. For example, minimum sleep it will is anywhere between 2us to 2ms on exact same machine. If you ask for 1ms sleep, its not unusual to see it sleep for 3ms and so on. – Shital Shah Jan 25 '17 at 19:56
  • 2
    @ShitalShah do you have any references for that? Is that a standard problem or an implementation problem? – Stephan Dollberg Jan 25 '17 at 21:10
  • This is old, but I came across this while working on something that uses std::chrono. sleep_for is definitely not very accurate and it seems to get worse the larger the sleep period gets. You can verify this yourself. Here are the results of my tests under MSVC 14. ` Target: 1000 Actual: 3800 Target: 1000000 Actual: 1066700 Target: 16000000 Actual: 16671100 ` All values are in nanoseconds. "Target" is the time passed into sleep_for and "Actual" is the actual time passed. – Underdisc Sep 27 '20 at 15:09
4

You should also notice that there is the scheduler, which probably allows no sleeps that are shorter than an timeslice (somewhat around 4 ms - 10 ms, depending on your windows and machine). sleeping less than that is not possible on

Here are some (quite old) research on that issue windows.

This article suggests using Win32 timeBeginPeriod() to achieve that.

Dan D.
  • 73,243
  • 15
  • 104
  • 123
Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113
  • also, the correct term is "quantum" (in lieu of timeslice), which is the amount of time the windows scheduler allocates for a thread to run. additionally, the quantum setting of the kernel does not necessarily imply a minimum sleep time, it's possible the scheduler could pend and resume a thread in less than a single quantum (quantums are not used to enforce thread idle time, rather, they are used to limit thread running time.) – Shaun Wilson Feb 17 '15 at 05:10