DanielChappuis / reactphysics3d

Open source C++ physics engine library in 3D
http://www.reactphysics3d.com
zlib License
1.54k stars 223 forks source link

Multiple isolated worlds on individual threads. #149

Closed kix-mcotton closed 4 years ago

kix-mcotton commented 4 years ago

I have a bunch of simulations that should all run in isolation. I have it working on a single thread, updating each simulation one after the other. I'm hoping to move simulations to their own threads but I'm noticing some weirdness in the collision detection. Should there be any issues with updating, collision detection on multiple worlds in multiple threads given that each thread should have it's own unique state? Are there any static data/singletons that might cause issues? Thanks.

kix-mcotton commented 4 years ago

In poking around I see there's a lot of static data in MemoryManager which is probably the cause of the issues I'm seeing.

DanielChappuis commented 4 years ago

Yes in the current version of the library the memory allocators are static and shared between multiple worlds and there might be some issues if you are using some multi-threading here. Note that ReactPhysics3D doesn't officially support multi-threading yet.

First, can I ask you if you are noticing some weirdness in the collision detection only if you are using multi-threading ?

Maybe you can try to add mutexes at the beginning of the allocate() and release() methods of the different memory allocators (DefaultPoolAllocator and SingleFrameAllocator classses) to see if it helps. This is what will be done in the next release of the library.

kix-mcotton commented 4 years ago

Yes the problem only appears in multi threaded versions. In single threaded I've had no issues.

For the moment to get past the blocker I've put a mutex around the function that does all the physics work and that appears to solve the issue, although obviously locking out a big chunk of code makes a lot of the multi thread work redundant.

I saw in another issue that you were planning for 0.8.0 to support threading. Is that still the plan? Will that be coming out any time soon?

DanielChappuis commented 4 years ago

I initially planned to integrate multi-threading collision detection in version 0.8.0. Unfortunately, some other parts took me quite some time and therefore it won't be part of version 0.8.0. I will probably release v0.8.0 this week or the next one. In this release, I use some mutexes in the memory allocators and therefore, it might solve your issue.

kix-mcotton commented 4 years ago

That's unfortunate. But the good news is that putting the mutexes into the allocators seems to have made it behave. It's still early in testing but hopefully that will get the job done for now.

DanielChappuis commented 4 years ago

Ok perfect. I am closing the issue. Do not hesitate to reopen it if needed.