C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Timer Vs Sleep Posted by SS20 on 5 Jun 2006 at 10:44 PM
i have used Sleep(n) in my code but am finding that is very inaccurate with errors upto 40 millisec. I need to replace this by a timer in some way by using a while loop or something. can anyone help me out by giving some simple code i can read ot understand how to use timers.any help would be great..
Report
Re: Timer Vs Sleep Posted by stephl on 6 Jun 2006 at 1:44 AM
This message was edited by stephl at 2006-6-6 1:49:1

: i have used Sleep(n) in my code but am finding that is very inaccurate with errors upto 40 millisec. I need to replace this by a timer in some way by using a while loop or something. can anyone help me out by giving some simple code i can read ot understand how to use timers.any help would be great..
:
I do not know what precision you need, anyway you have to keep in mind that if you are working with a multitask system (like Windows), it is not so easy to wait for a very short time precisely. I have never tried this on a Windows OS and have no idea whether an error of 40ms is important or not.
I do not think using WM_TIMER messages would be more accurate because the delivery of window messages takes time and I read that the programmer should not expect the timer to be very accurate.

Steph
Report
Re: Timer Vs Sleep Posted by tsagld on 6 Jun 2006 at 2:03 AM
: This message was edited by stephl at 2006-6-6 1:49:1

: : i have used Sleep(n) in my code but am finding that is very inaccurate with errors upto 40 millisec. I need to replace this by a timer in some way by using a while loop or something. can anyone help me out by giving some simple code i can read ot understand how to use timers.any help would be great..
: :
: I do not know what precision you need, anyway you have to keep in mind that if you are working with a multitask system (like Windows), it is not so easy to wait for a very short time precisely. I have never tried this on a Windows OS and have no idea whether an error of 40ms is important or not.
: I do not think using WM_TIMER messages would be more accurate because the delivery of window messages takes time and I read that the programmer should not expect the timer to be very accurate.
:
: Steph

The WM_TIMER message is sent with the lowest priority possible. It is very inaccurate.
Look at the performance api's (QueryPerformanceCounter and QueryPerformanceFrequence for example). They may help you.
You must also give your thread high priority, to prevent Windows as much as possible from interrupting your process. But that slows other processes down.
Another option may be to use the Pentium's RDTSC instruction, which allows for clock-tick accuracy.


Greets,
Eric Goldstein
http://www.gvh-maatwerk.nl


Report
Re: Timer Vs Sleep Posted by stober on 6 Jun 2006 at 3:52 AM
: You must also give your thread high priority, to prevent Windows as much as possible from interrupting your process. But that slows other processes down.


I tried that once and it brought the entire os to a screeching hault because the os didn't even get any time.
Report
Re: Timer Vs Sleep Posted by Donotalo on 6 Jun 2006 at 6:06 AM
: : You must also give your thread high priority, to prevent Windows as much as possible from interrupting your process. But that slows other processes down.
:
:
: I tried that once and it brought the entire os to a screeching hault because the os didn't even get any time.
:
shudn't that thread stop at a certain time and return control to os?


~Donotalo()

Report
Re: Timer Vs Sleep Posted by tsagld on 6 Jun 2006 at 7:22 AM
This message was edited by tsagld at 2006-6-6 7:25:27

: : : You must also give your thread high priority, to prevent Windows as much as possible from interrupting your process. But that slows other processes down.
: :
: :
: : I tried that once and it brought the entire os to a screeching hault because the os didn't even get any time.
: :
: shudn't that thread stop at a certain time and return control to os?
:

:
~Donotalo()
:
:

There are about 56 different priority levels, based on the thread priority and the priority of it's parent process.
There is for example the THREAD_PRIORITY_TIME_CRITICAL priority which will cause the effects stober described.
But there's also THREAD_PRIORITY_HIGHEST and THREAD_PRIORITY_ABOVE_NORMAL. And more, MSDN describes them all.

Greets,
Eric Goldstein
http://www.gvh-maatwerk.nl







 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.