Multi-Thread example

Post Reply
martin_st
Posts: 6
Joined: Thu Aug 19, 2010 1:59 pm
Location: Erlangen / Germany

Multi-Thread example

Post by martin_st » Tue Jan 22, 2013 10:15 pm

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

User avatar
atena
Site Admin
Posts: 112
Joined: Fri Jun 04, 2010 1:25 pm

Re: Multi-Thread example

Post by atena » Wed Jan 23, 2013 5:34 pm

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.
Angel Tena
Head of RealFlow Technology
Next Limit Technologies

martin_st
Posts: 6
Joined: Thu Aug 19, 2010 1:59 pm
Location: Erlangen / Germany

Re: Multi-Thread example

Post by martin_st » Wed Jan 23, 2013 10:26 pm

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

Post Reply