Link to home
Start Free TrialLog in
Avatar of yossikally
yossikally

asked on

sleep(1) - how long does it treally take?

Working on NT 4 (embedded version if it makes a difference), I know this is not a real time system, but I wonder how close I can get.

In the team we are debating whether sleep(1) really returns after 1 ms - some say it is more likely to return after 10 ms, and that it is not predictable at all.  

To summarize: what is the best way to write a thread that is not in highest priority, and has to wait for exactly 1 ms (or as close as possible) in between doing some work.
Avatar of NickRepin
NickRepin

>>: what is the best way to write a thread that is not in highest priority

Use SetPriorityClass() and SetThreadPriority()

>>and has to wait for exactly 1 ms

It is not possible in principle. A time slice for a thread is ~20 msec.
From MSDN library:

<<Multitasking

....

The length of the time slice depends on the operating system and the processor. Because each time slice is small (approximately 20 milliseconds), ....>>
>>some say it is more likely
to return after 10 ms, and that it is not predictable at all.  

That's true.
Avatar of yossikally

ASKER

... So why is it possible at all to call sleep(1), is it just MS being somewhat misleading??
Avatar of Paul Maker
the sleep(mil) API says that the caller will sleep for ATLEAST mil time not less than. therefore microsoft are not misleading
... well they could as well tell you to that the parameter is in nano seconds, and not be misleading.  Has anyone used multimedia timers?  Someone told me they can help you get the cpu faster
yossikally ,

I've done some experiments on Mutimedia timers. Yes it is more accurate when it is periodic. Meaning- if you're asking for 100ms period, the standard NT timer (SetTimer) won't give you better accuracy. But it appeared to me like the multimedia timer algorithm constently evaluate/correct the timer frequency, so that the application get a "better" timer ticks on avarage.

But I dont think it applies to yossikally's question. If you need smaller/accurate timers, you might need to use the profiling method.
ASKER CERTIFIED SOLUTION
Avatar of robpitt
robpitt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yossikally: what's your actual Q?

a) does Sleep(1) sleep exactly 1 ms ? (A:No)
b) How can I sleep for exactly 1 ms ? (A:not guaranteed, but robpitt is closest)
c) Is Windows a real-time OS (A:No)
d) Can I rely on *any* timing in Windows (A: No)

my question is not if i can do it.  I understand I can't, but how close i can get.  Given several options (sleep(1), sleep(0) inside a loop, SetWaitableTimer, Set MultimediaTimer, etc.) what is the one that would get me closest to resume operation after exactly 1 ms.?

The application is in the digital video area over network, and more specifically - ensuring 'smooth' flow of data over the network. Tha's why I need short intervals in between sending packets.  Still, it has to be guaranteed.
>>Given several options
(sleep(1), sleep(0) inside a loop, SetWaitableTimer, Set MultimediaTimer, etc.) what is the one that
would get me closest to resume operation after exactly 1 ms.?

Does not matter. It makes no sense to talk about "exactly 1 ms" in the Windows environment. Use sleep(0) or sleep(1) as the easiest way.
Well, Nick, sometimes "closer" is better than "easier".
From all what I learnt, using multimedia timers will give you best results.

A multimedia timer (TimeSetEvent) is just another schedduled thread so it too can be late.


Anyway, re:
   "The application is in the digital video area over
    network, and more specifically - ensuring 'smooth'
    flow of data over the network. Tha's why I need
    short intervals in between sending packets.  Still,
    it has to be guaranteed"

I'm not sure that trying to spit out a particular amount of data every 1ms will ever work on windows. Thats really only practicle on a real time system.

Is it not possible to introduce buffering?
since the system clocks don't go more precise than >10ms, and the rest (thread switching and so on) is probably determined by instruction/clock tick averages, i doubt that there's any way to get 1ms precise except by doing a performance check to the system at the startup of you prog determining the loop-count needed for a, may be asm @loop: nop loop @loop. but remember, that's also the cause of the division by zero in older games, so i guess you should do something intelligent at the startups performance test.