Jump to content
  • Advertisement
Sign in to follow this  
Ryan_001

C++/Win32 timers

This topic is 2530 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to set up a timer for a window manager/wrapper to be used for basic game logic/animation. In the past I've always used timeGetTime(). I was looking around to see if there was a better way to go about it. What I need is a timer that is relatively stable (doesn't occasionally/randomly run backwards), millisecond or better precision (microsecond precision if possible, but anything sub-milli would be great), and can be called from any thread. Functions I've looked at are:

timeGetTime() - worked ok before but I'd like better accuracy, also http://blogs.msdn.co...imegettime.aspx states that using [font=Arial][color=#333333]

timeBeginPeriod(1) on Win7 is bad.[/font]



QueryPerformanceCounter() - very good accuracy, but the MSDN remarks seem to imply this isn't multi-thread safe, also http://www.virtualdu...ntry.php?id=106 brings up a bunch of issue with it (its rather old link though so perhaps these have been fixed?).



What do you guys generally use? Any thoughts or suggestions?

Share this post


Link to post
Share on other sites
Advertisement
There is indeed a lot of conflicting information out there and it's hard to know who to believe. So I have code that uses QueryPerformanceCounter by default but can be switched to timeGetTime() just in case. FWIW, I haven't had any problems with QueryPerformanceCounter yet.

Share this post


Link to post
Share on other sites
Depending on what you need to do, you might consider one of those alternatives (some are already also in the C++11 Standard):
http://www.boost.org/doc/libs/release/doc/html/date_time/posix_time.html
http://www.boost.org/doc/libs/release/libs/timer/doc/cpu_timers.html
http://www.boost.org/doc/libs/release/doc/html/chrono.html

See also:
http://stackoverflow.com/questions/8386128/how-to-get-the-precision-of-high-resolution-clock
http://stackoverflow.com/questions/538609/high-resolution-timer-with-c-and-linux
http://www.stdthread.co.uk/doc/headers/chrono/high_resolution_clock.html
http://en.cppreference.com/w/cpp/chrono/high_resolution_clock

Share this post


Link to post
Share on other sites

Depending on what you need to do, you might consider one of those alternatives (some are already also in the C++11 Standard):
http://www.boost.org...posix_time.html
http://www.boost.org...cpu_timers.html
http://www.boost.org...tml/chrono.html


To my knowledge boost chrono uses [color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

QueryPerformanceCounter() under the hood, boost posix time uses ftime(), and even though I didn't see it specified anywhere boost timers I imagine uses

[/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

QueryPerformanceCounter() as well? I still have concerns with

[/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

QueryPerformanceCounter() as stated above (not thread safe, might have issues with frequency fluctuations) and I've seen ftime() mentioned many places but I've found very little info on its implementation/limitations.

[/font]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

I'll read through those links though thank-you.

[/font]

Share this post


Link to post
Share on other sites
Yeah, I think at least system_clocks in Boost.Chrono do, looking at the source seems to confirm (there are some interesting notes about the QueryPerformanceCounter in the comments): http://svn.boost.org/svn/boost/trunk/boost/chrono/system_clocks.hpp
You may also consider looking at: http://msdn.microsoft.com/en-us/magazine/cc163996.aspx

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!