Page 1 of 1

Multi-Thread example

Posted: Tue Jan 22, 2013 10:15 pm
by martin_st
Hi there,

is there an example or documentation about how to synchronize multiple threads in a particle plugin by the RF Mutex Object available? The SDK documentation isn't very detailed on this issue.

Thank you very much,
Martin

Re: Multi-Thread example

Posted: Wed Jan 23, 2013 5:34 pm
by atena
Hello, you must have a mutex object as a member variable of your plugin, this way the same mutex is shared and used by all the threads that your plugin is spawning.

Code: Select all


using namespace nextlimit::rf_sdk;

class MyParticleSolverPlgSdk : public ParticleSolverPlgSdk
{
     Mutex mutex_;

     virtual void computeInternalForces( ParticleSolver* particleSolver,
                                          PB_Emitter* emitter, 
                                          int nThread, 
                                          RFEmitter_It iter ) 
     {
          mutex_.lock();
          // SAFE ACCESS TO SHARED RESOURCE
          mutex_.unlock();
     }

};
I hope this helps.

Re: Multi-Thread example

Posted: Wed Jan 23, 2013 10:26 pm
by martin_st
Hello,

thanks for your really helpful reply. One question is still left: what about, if I want to make sure, that all threads are waiting until a certain passage is finished by the last of them? To become a little more detailed, supposed I have a member object o of a class X, which is constructed in precomputeInternalForces([...]). O knows about how many threads are used during simulation from scene.getNumberOfThreads(), which is passed as a constructor parameter. X defines a method

Code: Select all

public: void doSomething(int threadId) 
{
this->subA();
this->subB();
}
o.doSomething(nThread) is called by computeInternalForces,

and two private methods

Code: Select all

private: void subA(){...} 
private: void subB(){...}   
How do I make sure, that no thread starts executing subB() unless all the other threads have finished executing subA()?

Thanks a lot in advance,
Martin