Multithreading and Timing

Multithreading and Timing

Hello everyone,

i do have a question regarding multithreading and timing.
Right now i managed to encapsulate the simulation with havok in an extra thread, as well as the rendering to another one.
I did a little thinking and came up with these thoughts:
When havok has a frequency of 60Hz (step size = 0.0166f), subsequential calls to stepDeltaTime should be at the same frequency. So i do a time measure routine and if the thread didn't need the 0.0166 seconds i put him to sleep for the remaining time. If the simulation takes longer than the 0.0166 seconds i put the thread for 1ms to sleep. The simulation looks quite good with that configuration.

Question 1: Is that correct or did i overlook something?
Question 2: It happens that the thread (with the stepDeltaTime) takes longer than the 0.0166 seconds. In fact it doesn't matter what the stepsize is. Is that a normal/acceptable behavior (the deviation is quite small)?

Thank you,
Frederic-Felix

2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Hi Fredric-Felix,

Just curious? What type of application are you building?

It might make sense to use a "main" or "game" thread to control several "worker" threads. With this model you have very strict control over what happens when.

Control over program flow is important if you want to ask the physics world questions via the query interfaces (ray casting, linear casting, get closest points, ect). The reason why this is important is because you need to ensure that your physics thread(s) are not stepping the world while you're asking these questions; otherwise, in the best case you'll get incorrect results or in the worst case you'll get a crash.

The easiest way to manage this is to not have the physics engine in another thread. You can have worker threads that process physics but this processing is initiated by the main, or game, thread. This isn't the only way but its very common because of its simplicity.

You could change your model to this approach by removing the timer and replacing it with a semaphore. The semaphore would be released by the main thread when physics should be evaluated and acquired before you sync the transform from physics to your game objects.

Jason

Leave a Comment

Please sign in to add a comment. Not a member? Join today