How can I block a thread for nanoseconds or microseconds? The Sleep()
function is not possible because it accepts miliseconds which is obviously not what I need.
Asked
Active
Viewed 1,444 times
0

user3060326
- 187
- 2
- 16
-
4Do not expect to get such precision on Windows (nor any of the platforms supported by Delphi XE5). If you're going to control some device, use a microcontroller, or control it from some RTOS. – TLama Mar 27 '14 at 22:41
-
1Windows is not a real-time OS. and neither are OSX/Android/iOS. There is no sleep function I'm aware of with that level of granularity. It might be better if you explain what it is you're trying to accomplish with your thread to see if there is a better solution than blocking/sleeping instead. – Ken White Mar 27 '14 at 22:45
-
I doubt that you can sleep like that. The best you can do is spin. Hardly the same. – David Heffernan Mar 27 '14 at 22:49
-
Possible duplicate (different language) of http://stackoverflow.com/questions/6768975/windows-c-and-possibility-of-a-microsecond-sleep – Graymatter Mar 27 '14 at 23:04
-
Windows? You have to use multimedia timer for that. – Free Consulting Mar 28 '14 at 00:43
2 Answers
5
Use the TStopWatch class, and write a while
loop that spins until the desired number of ticks (100 nanosecond intervals) has elapsed.
property ElapsedTicks : Int64 read GetElapsedTicks;
This will not relinquish control to other threads; it will simply wait in the current thread for the desired period of time. There will be some degree of error; the amount of error will depend on how long it takes Delphi to execute each loop.
Further Reading
How to Accurately Measure Elapsed Time Using High-Resolution Performance Counter

Robert Harvey
- 178,213
- 47
- 333
- 501
-
-
It is a timed busy loop. Was that not what you were looking for? – Robert Harvey Mar 27 '14 at 22:59
-
The frequency of a tick is not hardcoded and can be read from the Frequency property – David Heffernan Mar 27 '14 at 23:01
-
@user3060326 I would think that you have to use a timed busy loop. Relinquishing control to the OS would mean that you can't guarantee that you will get back control when you need it. – Graymatter Mar 27 '14 at 23:01
-
@user3060326: Yes, but only for a very short period of time. Windows typically allocates 20ms time slices to each thread, so you have to retain control of the thread if you want a delay interval smaller than that. On a multi-core machine (which most machines are, nowadays), you won't even notice the delay. – Robert Harvey Mar 27 '14 at 23:02
-
1This is a very sensible reply. Generating a real-time pulse of say 500 us via a memory or I/O mapped chip (e.g a PCI card) is quite normal and tying up the CPU is the way to go for such short times. You wont stop pre-emptive multitasking making the time longer, but it will not be any shorter. – Brian Frost Mar 28 '14 at 12:06
5
Windows does not support nano sleep. The thread scheduling used by Windows is much coarser than that. The Windows thread quantum is orders of magnitude longer than a nano-second.

David Heffernan
- 601,492
- 42
- 1,072
- 1,490
-
1In practical tests using GetTickCount I've always seen it skip with 12 or 15 milliseconds at least. (Get a load of this: http://msdn.microsoft.com/en-us/library/aa450636.aspx ) – Stijn Sanders Mar 28 '14 at 09:54
-
@StijnSanders yes, and on windows XP, expect up to 50-55 ms, IIRC on windows 7, it's 15 ms, not sure about vista and windows 8 – Mar 28 '14 at 18:36