C/C++ windows Sleep, and timers.

Discussion in 'Division by Zero' started by slicer4ever, Jul 4, 2011.

  1. slicer4ever

    slicer4ever Coding random shit

    Joined:
    Nov 17, 2008
    Messages:
    1,444
    Likes Received:
    3
    Last played:
    Destiny on Xbox One
    hello all, i'm working on an application for windows which implements a timer class that uses clock based timers, and calls Sleep at the end of the primary loop to utilize as little cpu as possible.

    the problem is that calling sleep produces bad results from my timer code, i've determined this is because the time that you pass to sleep is only the minimal time that the system has to wait before giving back control to my program, so it may be longer than 1 ms, and can produce varied results.

    so my problem lies in that i don't want to utilize 100% cpu when my program obviously doesn't take that long to execute, but using Sleep destroys the accuracy of my timers(i've implemented high performance counter's as well, but they suffer the same problem).

    is there any way i can relinquish control to the os, but not lose accuracy in my timers?

    edit: realized my solution, instead of reseting the amount of time elapsed to 0 when i detect the timer has finished, i instead subtract the timelength, so if the time ran is > than time length it compensates for that, so ignore my post, solution discovered.
     

Share This Page