[ODE] multihreading and ODE

Chunky Kibbles chunky at icculus.org
Mon Jan 24 13:22:15 MST 2005


On Mon, Jan 24, 2005 at 02:45:26PM +0100, cesar pachon wrote:
> hello! I put a separate thread (timer) for
> fixed-stepping ODE, and a Main thread with
> update-paint code.. the problem is that periodically
> my car (a simple car simulation) vibrates. it quicky
> becomes stable, but graphically looks bad. my code is:
> 
>  
>  timer_func(){
>    stepping_ode = true; 
>    dWorldQuickStep (dWorldID, fixedstepsize);
>    stepping_ode = false;
>  }
> this method runs a 30 fps. My Update and Paint thread
> runs at 120 fps (no constant rate) and I check the
> flag:
>  update(){
>   //calculate actual framerate.. 
>   if(!stepping_ode){
>     //query ODE and update graphic objects.. pos and
> rot.
>   }
>   paint();
>  }
> my question is.. is something bad with the flag? do I
> need sync also the reading method? If I do that, it
> would affect the fixed framerate of ODE stepping
> function?
> thanks by the help!  

Yes, there's something bad with the flag. Consider the case where
stepping_ode is false.
if(!stepping_ode){
    //query ODE and update graphic objects.. pos and rot.
}

This will run. Now, if you enter this function and the other thread
executes for a while...
timer_func(){
  stepping_ode = true; 
  dWorldQuickStep (dWorldID, fixedstepsize);
  stepping_ode = false;
}

It's possible for dWorldQuickStep to start running, even thought you're
updating the screen. This is, though, unlikely to be a real problem [you
need to fix it anyways].

If you're on a *nix alike, use a pthread_mutex:


pthread_mutex_t stepping_mut;
pthread_mutex_init(&stepping_mut, NULL);

timer_func(){
 pthread_mutex_lock(&stepping_mut);
 dWorldQuickStep (dWorldID, fixedstepsize);
 pthread_mutex_unlock(&stepping_mut);
}

pthread_mutex_lock(&stepping_mut);
// query ODE and update graphic objects.. pos and rot.
pthread_mutex_unlock(&stepping_mut);



If you're on anything that doesn't support posix threads, I don't know
how to help you.

In practice, you should consider threads the wrong solution to the
problem [if you didn't know about mutexes, they're DEFINITELY the wrong
solution to the problem]. I'd urge you to do the above in a single
thread.

I suspect that this isn't real real problem, by the way. It's more
likely something else happening, but I've no idea what that might be.
Check that your bodies are being disabled correctly.

Gary (-;


More information about the ODE mailing list