Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2001-07-19
18
Medium Priority
?
352 Views
Last Modified: 2013-12-03
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.
0
Comment
Question by:yossikally
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +4
18 Comments
 
LVL 15

Expert Comment

by:NickRepin
ID: 6297514
>>: 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.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 6297517
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), ....>>
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 6297530
>>some say it is more likely
to return after 10 ms, and that it is not predictable at all.  

That's true.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:yossikally
ID: 6297550
... So why is it possible at all to call sleep(1), is it just MS being somewhat misleading??
0
 
LVL 10

Expert Comment

by:makerp
ID: 6298550
the sleep(mil) API says that the caller will sleep for ATLEAST mil time not less than. therefore microsoft are not misleading
0
 

Author Comment

by:yossikally
ID: 6299407
... 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
0
 
LVL 8

Expert Comment

by:mxjijo
ID: 6299657
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.
0
 
LVL 5

Accepted Solution

by:
robpitt earned 200 total points
ID: 6302818
I don't think there is a way to sleep for exactly 1ms.

If you must wait for 1ms exactly you have little choice but burn cpu cycles in a loop until the time has ellapsed.

However I doubt you really need to do this - what is the task at hand anyway?

Rob
0
 
LVL 15

Assisted Solution

by:NickRepin
NickRepin earned 200 total points
ID: 6303718
>>If you must wait for 1ms exactly you have little choice but burn cpu cycles in a loop until the time
has ellapsed.

While this loop you can be suspended for some time to let other threads go.
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6304338
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)

0
 

Author Comment

by:yossikally
ID: 6305683
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.
0
 
LVL 15

Expert Comment

by:NickRepin
ID: 6305715
>>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.
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6305840
Well, Nick, sometimes "closer" is better than "easier".
From all what I learnt, using multimedia timers will give you best results.

0
 
LVL 5

Expert Comment

by:robpitt
ID: 6305912
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?
0
 
LVL 5

Expert Comment

by:drnick
ID: 7497646
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.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question