Timer Vs Sleep - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Timer Vs Sleep

SS20SS20 Posts: 1Member
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..

Comments

  • stephlstephl Posts: 422Member
    [b][red]This message was edited by stephl at 2006-6-6 1:49:1[/red][/b][hr]
    : 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
  • tsagldtsagld Posts: 621Member
    : [b][red]This message was edited by stephl at 2006-6-6 1:49:1[/red][/b][hr]
    : : 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


  • stoberstober Posts: 9,765Member ✭✭✭
    : 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.
  • DonotaloDonotalo Posts: 715Member
    : : 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.
    :
    [purple]shudn't that thread stop at a certain time and return control to os?
    [/purple]
    [hr][purple]~Donotalo()[/purple]

  • tsagldtsagld Posts: 621Member
    [b][red]This message was edited by tsagld at 2006-6-6 7:25:27[/red][/b][hr]
    : : : 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.
    : :
    : [purple]shudn't that thread stop at a certain time and return control to os?
    : [/purple]
    : [hr][purple]~Donotalo()[/purple]
    :
    :

    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




Sign In or Register to comment.