Is there a way to use Dynamics World as a thread safe?

Post Reply
kcjsend2
Posts: 15
Joined: Wed Jun 30, 2021 9:10 am

Is there a way to use Dynamics World as a thread safe?

Post by kcjsend2 »

I try to use Bullet physics on my IOCP server, but there's a problem
Because btDiscreteDynamicsWorld does not thread safe, it is necessary to use Lock, which causes a lot of performance loss.
Is there a way for me to stepSimulation safely without using Lock?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Is there a way to use Dynamics World as a thread safe?

Post by drleviathan »

Bullet itself does not ask for a lock on anything, therefore if you are experiencing contention performance problems on the lock around the world->stepSimulation() then you're doing it wrong. You need to either (a) use finer grained locks or (b) use some other technique (e.g. double-buffering or data-throwing) to reduce your lock durations.

If the advice above is not clear, and you want to share an outline of your architecture (e.g. what data you're locking and why) I'd be willing to offer more specific advice about how to speed it up.
kcjsend2
Posts: 15
Joined: Wed Jun 30, 2021 9:10 am

Re: Is there a way to use Dynamics World as a thread safe?

Post by kcjsend2 »

Thank you for your answer.
What I'm mainly worried about right now is Context Switching while running Step Simulation.
This is because if Context Switching to another thread occurs while Step Simulation is being performed, and if a rigid body is added, deleted, or referenced from a changed thread, there may be a problem.

I know I can do Lock to solve this problem, but I'm asking because I think it will be the best performance to make most of the functions thread safe.

Is btDynamicsDiscreteWorldMt class designed to prevent this? I don't know if I understand correctly.

When I saw the mention of double buffering, is there a way to copy all the information in one Dynamics World (gravity value, included rigid bodies, vehicles...) to another Dynamics World?
If possible, it would be nice to keep the pointer to the rigid body referenced from the outside and the pointer to the vehicle from the outside available.

If you find grammar awkward, it's because of machine translation. I'm sorry for the poor grammar.
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Is there a way to use Dynamics World as a thread safe?

Post by drleviathan »

btDynamicsDiscreteWorldMt is for multithreaded world->stepSimulation() computation.

If you just want to run world->stepSimulation() in its own single thread you definitely can't be adding/updating/deleting RigidBodies during the step --> that would cause crashes and/or unexpected bugs. What I have done in the past is: on thread0 (user/network input thread) I accumulate all change operations (adds/updates/deletes) onto lists. That is, I don't perform the operations but instead save the information required to do them. Then, once per frame I lock the mutex and "throw" those lists of pending operations over to thread1 (simulation thread) where they are done right before the step, and all that work is done without a lock because the lists being consumed there are not the ones where changes are accumulating on the other thread. The lock only happens when transferring the data to the other thread.

Note: in this architecture thread0 doesn't ever operate directly on RigidBodies. Instead it operates on GameObjects (the renderables) and all operations on corresponding RigidBodies are done in thread1.
tomson0119
Posts: 5
Joined: Tue Feb 08, 2022 10:18 am

Re: Is there a way to use Dynamics World as a thread safe?

Post by tomson0119 »

drleviathan wrote: Tue Feb 08, 2022 5:08 am btDynamicsDiscreteWorldMt is for multithreaded world->stepSimulation() computation.

If you just want to run world->stepSimulation() in its own single thread you definitely can't be adding/updating/deleting RigidBodies during the step --> that would cause crashes and/or unexpected bugs.
What if my program runs multiple threads , let say 4 threads and all thread randomly access to btDynamicsDiscreteWorldMt->stepSimulation()..
Is it data race or does it cause crash/bug?

I also use multiple btRaycastVehicle and multitple rigidbodies, and also threads randomly access to these. inserting, updating, deleting.
So I'm afraid it would be data race or crash/bug too..

It's because I'm using IOCP architecture. IOCP manages thread pool containing multiple threads.. I have no idea which one to pop up and execute operations..(But I can find thread id.)
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Is there a way to use Dynamics World as a thread safe?

Post by drleviathan »

What if my program runs multiple threads , let say 4 threads and all thread randomly access to btDynamicsDiscreteWorldMt->stepSimulation()..
Is it data race or does it cause crash/bug?
Yes, that would cause crashes and bugs: it is a Bad Idea.
I also use multiple btRaycastVehicle and multitple rigidbodies, and also threads randomly access to these. inserting, updating, deleting.
So I'm afraid it would be data race or crash/bug too..
Correct.
It's because I'm using IOCP architecture. IOCP manages thread pool containing multiple threads.. I have no idea which one to pop up and execute operations..(But I can find thread id.)
You have identified a fundamental problem with multithreaded algorithms. Good luck.
Post Reply