OGRECave / ogre-next

aka ogre v2 - scene-oriented, flexible 3D C++ engine
https://ogrecave.github.io/ogre-next/api/latest
Other
1.09k stars 235 forks source link

ParticleSystem2 has many bugs when use Multi-thread。 #480

Open 36yang opened 3 days ago

36yang commented 3 days ago

System Information

Detailled description

When I create a multi-threaded(4 thread) scene manager and use the particleSystem2 as Sample, waiting for 3-5 minutes will randomly encounter many problems, such as throwing multiple exceptions or encountering particles Only one particle can be emitted at a time when the upper limit is reached, and there are actually no live particles.

Ogre.log

Callstack

Here is my screenshot:

QQ图片20241122104145

darksylinc commented 3 days ago

Hi!

If I understood correctly, all I need to do is set the number of threads to 4 and wait a few minutes for the PFX2 Sample to crash?

I'm going to need a way to repro otherwise (e.g. by modifying the Sample to trigger the crash).

Cheers

36yang commented 3 days ago

Yes, I can reproduce it 100% like this。 GZ7`BG6%BNC`@6QKFCJ_4{I

darksylinc commented 3 days ago

Thanks!

I was able to repro the problem.

Thread Sanitizer located the issue too.

ParticleSystemManager2::_updateParallel runs in two stages:

  1. Emit new particles
  2. Run emitters to all particles and tickParticles()

The problem is that the particles emitted in stage 1 by thread A may be in the middle of the range processed by stage 2 in thread B; thus the state of the particle becomes corrupt as A wants to reuse an inactive particle (to activate it) while B should be skipping that particle but seens it's suddenly active (because of A).

I'll need to think this through on how to fix it.

36yang commented 3 days ago

It's great to be able to find the problem. Multithreading is giving me a headache and I'm waiting for it to be fixed.