Jump to content
  • Advertisement
Sign in to follow this  
helmish

Multicore Java gameloop

This topic is 3349 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

Hello, I have recently run into a problem with my game loop that I can't seem to find a solution to. I have a game loop which used to use System.currentTimeMillis() for all time related issues (timing, sleeping, etc). But that wasn't good enough since on windows the granularity of this is 15ms and i was getting bad results. So I started using System.nanoTime() and LockSupport.parkNanos() (for sleeping) and i noticed i am getting negative time intervals between consecutive calls to nanoTime (due to my multicore processor). So my question is this: how is this normally handled? How can I accurately measure time with 1ms accuracy? How does the typical gameloop work around these two problems? Thanks in advance for any help and I hope this question is not too newbie-ish.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by helmish
So I started using System.nanoTime() and LockSupport.parkNanos() (for sleeping) and i noticed i am getting negative time intervals between consecutive calls to nanoTime (due to my multicore processor).


Looking at the docs, nanoTime() should consistently return time relative to an arbitrary fixed point. If you're seeing consecutive calls to nanoTime() in the same thread returning significantly (not just a few nanoseconds) *decreasing* values, that's obviously a big problem. The value returned may be negative, but it should be moving towards zero (ie, increasing) with consecutive calls.

If you're getting values that seem reasonable but are negative, you probably just need to take the absolute value of the difference. If you're getting values that are wildly off, then something else is going on.

Share this post


Link to post
Share on other sites
Im sorry, my bad.
I didn't mean I am getting negative return values but rather negative delta values.

i.e.

time1 = nanotime()
... do a little bit of stuff
time2 = nanotime()

time2 - time1 < 0

This happens because each cpu/core holds it's internal "nanotime" or whatever and they are not synchronized.

the values returning from nanotime is based on whatever cpu/core the thread is running on at the moment.

this is a known issue with nanotime and multicore processing which, in my opinion, renders this method useless.

is there a way to restrict the jvm to run on only 1 cpu/core?

Share this post


Link to post
Share on other sites
here's a reference to that "known issue" i mentioned about nanotime.
in case anyone's interested

http://mindprod.com/jgloss/time.html

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!