开发者

Strange stuttering issue with multithreading

开发者 https://www.devze.com 2023-03-08 17:51 出处:网络
I\'ve modified my physics from the last post: Updating physics engine ina separate thread, is this wise? like so:

I've modified my physics from the last post: Updating physics engine ina separate thread, is this wise? like so:

public void PhysicsThread()
{
    int milliseconds = TimeSpan.FromTicks(333333).Milliseconds;
    while(true)
    {
        System.Threading.Thread.Sleep(milliseconds);

        world.Step(milliseconds / 1000.0f);
    }
}

As before it's running in its own thread. I'm finding something odd which I'm not sure I quite understand, it's a b开发者_Python百科it difficult to explain so I made a video recording of it: http://www.youtube.com/watch?v=qFf6oSRfVt8

If you look carefully you can see the object being fired from the cannon occasionally stutters and appears to move backwards a set amount of distance. This is only noticable on fast moving objects (such as the projectile being fired).

This has baffled me completely. I've even created a system where I cache the position of bodies in the game logic thread so that as the physics thread may update bodies positions it won't affect the positions being read in by the game logic until an update function is called within the game logic thread which updates the physics bodies positions for the game logic to read.

Any ideas what aspect of multithreading may be causing this issue? I don't think it's the physics engine updating the bodies positions during game logic and drawing as I've alrady mentioned I cache that and it remains constant throughout...


My first guess is that you may have a classic race condition, where multiple threads are competing to update the object's position without locking or ordering guarantees.

You can check Wikipedia to learn more about race conditions, locking, and other fundamentals of multithreading/multiprocessing.

It's hard to say more without seeing more of your code, especially the part that does the update.

Edit: One thing you can do is to store DateTime.Now on each loop, and compare it with the previous value. If your time delays are inconsistent you'll see it.

Another thing to check is to see how long your world.Step() function is taking to execute (again using DateTime.Now and some logging, or whatever).

If both of these indicate consistent timing, then my suspicion would fall on the physics engine. Check the position of the object before and after calling world.Step(), and if you see any weird jumps that should tell you where to look.


This should probably be a comment, but it would be difficult to post code. If you're trying to do realtime then this code is prone to time jitter as there's no guarantee that Thread.Sleep will sleep for the perscribed time. I'd use a stopwatch to measure elapsed time and use that to drive your world.

public void PhysicsThread()
{
    int milliseconds = TimeSpan.FromTicks(333333).Milliseconds;
    var stopwatch=System.Diagnostics.Stopwatch.StartNew();

    while(true)
    {
        System.Threading.Thread.Sleep(milliseconds );

        world.Step(stopwatch.ElapsedTicks);
        stopwatch.Restart();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消