?
Solved

Sleeping for less than 1 millisecond in c++/Windows

Posted on 2011-04-30
17
Medium Priority
?
4,399 Views
Last Modified: 2012-05-11
Hello :)
I'm trying to delay my thread for less the 1 millisecond in C++Windows.
I've read here that using "select()" my not be suitable because it may wait a lot longer than what I need (50-100 mcirosec is perfectly fine).

I also read that the multimedia timer may do the job, so I found this article
http://www.codeguru.com/cpp/w-p/system/timers/article.php/c5759/
which is supposed to wait x microseconds, but when I try to use it twice consecutively, only the first call waits.
(I read here that Multimedia timers also have milliseconds resoulution, so I a little bit confused and will be happy if someone clarifies this.

Is there any other way to do this?
thanks :)

0
Comment
Question by:UltraDog
  • 7
  • 4
  • 2
  • +2
17 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 35497659
For that fine of delay, you may consider doing a busy loop, because any call into a system call may delay longer than 1 millisec or be unpredictable.
0
 
LVL 1

Author Comment

by:UltraDog
ID: 35497685
I was hoping to avoid a busy loop at any cost :(
0
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 664 total points
ID: 35497736
Try profiling (timing) some of the time related Win32 calls (http://msdn.microsoft.com/en-us/library/ms725473(v=vs.85).aspx) to see how fast they return on average. It may be that you can use one of them (even in a loop) for an effective higher resolution delay, without busy waiting.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Assisted Solution

by:Mechanic_Kharkov
Mechanic_Kharkov earned 668 total points
ID: 35497803
Multimedia timer is the only way to use microseconds intervals. But mrjoltcola is quite right that any system call can take unpredictable time. So, if you need very accurate measurement then you'll need a circle 0 driver to handle your timer.
And what about 1ms resolution of MMS timer from above mentioned article - this step is chosen for that sample just to illustrate thread sync technology. It's not a dogmatic value :-)
0
 
LVL 1

Author Comment

by:UltraDog
ID: 35498260
Hmm... so if I alter this program somehow, it's supposed to do exactly what I want? :)
0
 
LVL 9

Expert Comment

by:Orcbighter
ID: 35499901
What is this critical bit of code doing that it cannot be blocked for more than 1 millisecond?
You mention the "Select()" statement, which typically means dealing with sockets.
If this is so, then why not bypass the whole problem and have the select statement looping in its own thread. If the select fires, take the connection/data/whatever and run the work in a second thread. In this way the select can block all it wants and it will not affect the rest of your application.
0
 
LVL 1

Author Comment

by:UltraDog
ID: 35499919
I'm monitoring another process and getting continuous data, but I want to sample it only every X microseconds.
It has nothing to do with sockets. I wrote about the "select()" function because I've seen that people suggested it in order to wait a few microseconds. :)
0
 
LVL 9

Expert Comment

by:Orcbighter
ID: 35499969
OK, you just have to face the fact that, uder windows, 1 millisecond is the smallest you get.
When you suspend a thread, you are basically getting the OS to shedule a restart of your thread at a later time. This takes time (pardon the pun).
With functions like select(), the timeout parameter is the minimum time taken for that function to block. It is not a guarantee that it will not take longer.
Have you sat down and asked yourself why you need to sample at say 0.25 millisconds rather than 1 millisecond, or 2, etc. Maybe you need to rethink your solution, especially if you are trying to push Windows to work outside its limits in a guaranteed and regular way.
0
 
LVL 1

Author Comment

by:UltraDog
ID: 35500062
If I sample once every 1 or 2 milliseconds, I'll lose too much data...
0
 
LVL 9

Expert Comment

by:Orcbighter
ID: 35500674
Can you modify the process you are monitoring to send you the data you need?
0
 
LVL 1

Author Comment

by:UltraDog
ID: 35500833
No... I can't.
It doesn't send me the data, I TAKE IT! :)
(It's a stack sampling profiler)
0
 
LVL 1

Author Comment

by:UltraDog
ID: 35500875
I also noticed the code in  this article I linked only supports 1 *millisecond* resolution... :/
0
 
LVL 5

Expert Comment

by:Mechanic_Kharkov
ID: 35502288
Some tricks with monitored process could be useful to sync sampling.
Maybe there is some possibility to hook some system entry points to sync data sampling with calling of these system entry points by monitored process;
Or there could be a possibility to inject own code (i.e. far jumps) directly to monitored code segments to intercept its execution in some key points;

If there is no possibility to hook monitored process, then the most reliable way to provide such accurate sampling resolution seems to be a kernel driver that handles delay and monitored process stack memory from ring0. It's the only way for accurate handling such short intervals under windows, imho.
In addition, a number of  debuggers like SoftIce, WinDbg and so on, use hardware breakpoints on memory writing. There is an ability to set such breakpoints from ring0 code using CPU's debug registers. These breakpoints are the best way to monitor stack without continuous polling. This way is not an easiest one, but it sounds like the most accurate one.
0
 
LVL 9

Expert Comment

by:Orcbighter
ID: 35503003
Hi Ultradog, with regard to your comment about the link to code only supporting 1 millisecond:
I draw youir attension to my comments in 35499969. This is a LIMITATION of the WINDOWS operating system.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 668 total points
ID: 35511310
Check boost's posix time and thread sleep as that has granularity to nanoseconds where you OS can support it. I've never actually had a requirement to sleep for such a short amount of time and I'm not sure what the specific guarantees are but it's worth taking a look.

http://www.boost.org/doc/libs/1_41_0/doc/html/thread/time.html
0
 
LVL 1

Author Comment

by:UltraDog
ID: 35511725
But I'm using Windows, so I guess it's probably not supported...
0
 
LVL 40

Expert Comment

by:evilrix
ID: 35511794
Well, for sure Boost will provide that granularity. The details for compiler support can be found here

According to that, "By default the posix_time system uses a single 64 bit integer internally to provide a microsecond level resolution. As an alternative, a combination of a 64 bit integer and a 32 bit integer (96 bit resolution) can be used to provide nano-second level resolutions.". Under the compiler specific details there is nothing to suggest these granularities are not supported in Visual Studio.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Migration of Exchange mailbox can be done with the ExProfre.exe tool. But at times, when the ExProfre.exe tool migrates the Exchange Server user profile, it results in numerous synchronization problems. Synchronization error messages appear in the e…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

755 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