Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alternative to Sleep()

Status
Not open for further replies.

sweep123

Technical User
Joined
May 1, 2003
Messages
185
Location
GB
I need to wait for 40msec in a loop, what can I use (easily) as I understand that Sleep is not all that accuract.

In Visual Bacis I used High Performance Timers, but could not find any alternative for C++.

Any idears?
 
You can use the GetTickCount() which has millisecond
resolution.

For even higher resolution you can might (if your HW support it, but I think most new machines do) use
QueryPerformanceCounter() and QueryPerformanceFrequency()
Where the resolution depends on your CPU speed. I.e. on a 1GHz machine you get microsecond resolution.

QueryPerformanceFrequency returns the number of CPU ticks performed per second.

QueryPerformanceCounter returns the number of CPU ticks since startup.


Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
Yes thank you for that but how can you do a wait for say 50 msec and be sure thats its 50 msec.

I tried to adjust the sleep period if it overrun but would need to adjust to a negative value as overrun each time.

 
It just does not seem logical to me to believe that you can control thread time slicing at the millisecond level. If you could you would be wresting control of thread scheduling from the operating system.

"But, that's just my opinion... I could be wrong."

-pete
 
I agree with Pete. Windows is not a real time operating system, and therefore will not give interupts at precise time intervals. You can achieve some fairly predictable intervals with the above mentioned funtions, however the system has the last say as to when your thread gets cpu time. The time intervals attained should not be trusted for time critical operations.
 
OK, would the solution be to do away with the threads and use Multimedia timers to call the time critical section that each thread was doing.

i.e. a Multimedia timer for each critical section of code; and keep to a minimum the amount of processing required.

Would these timers get 80msec responses.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top