there will be a race condition. but that is very eassy to solve if you look at the demos you will see that each entity have a lightweigh mutex (a newton Mutex very important!!)
somethsi like this
- Code: Select all
class DemoEntity
{
// just code
// add a newton mutex
int m_lock;
// when using newton stuff from a call back, ex Get or Set Entrity Matroix from a call back
DemoEntity::GetMatrixForRender(DemoEntityManager& world, const dQuaternion& rotation, const dVector& position)
}
void DemoEntity::GetMatrixForRender(DemoEntityManager& world, const dQuaternion& rotation, const dVector& position)
{
// read the data in a critical section to prevent race condition from other thread
world.Lock(m_lock);
m_curPosition = m_nextPosition;
m_curRotation = m_nextRotation;
m_nextPosition = position;
m_nextRotation = rotation;
dFloat angle = m_curRotation.DotProduct(m_nextRotation);
if (angle < 0.0f) {
m_curRotation.Scale(-1.0f);
}
// release the critical section
world.Unlock(m_lock);
}
// these are the ligweght Newton Mutex implentation
- Code: Select all
inline void MyScene::Lock(unsigned& atomicLock)
{
while (NewtonAtomicSwap((int*)&atomicLock, 1)) {
NewtonYield();
}
}
inline void MyScene::Unlock(unsigned& atomicLock)
{
NewtonAtomicSwap((int*)&atomicLock, 0);
}
this code run amost unterrupted, bascially teh engien update matrices as fat as in can and teh render get the as facr as it can, and very rare ther is a collision
when the is a collsion because both teh engine an dteh render try to read the same matrix the the entiry Lock fail and call NetwonYeild and, wating until the matrxi is ready
however this engine does not stops there, if there are other pending entities, it continue upating those.
That code work for but all cases, sunc, asyn, sing wtil microthread and asyn and micor threaded.
for example you can set teh engioe to run Asyn and also use 8 microthreads.
Thsi si a level of parallesm that No other library has, not even Intell, or Mocrosoft products. Bascially teh engien runs with 98 to 99% with teh treaded not blocking each other, Thread collsion are lower that 1% on typical scenes
Not only that but teh engien also exposed the paraller excution of host application, with Pre and Post listeners.
for example say you have yor AI system, and you need to update 500 NPCs, or say 10000 particles, ot many sound etc.
you can plug a Pre or Post listener. Thsi gives you and update function with user data which is executed from the Newton update and is dispached in parallel from each micro thread.
There you can do you update without worry of thread collisions, and the update happens on simulation time. You can add as many listeners as you want.
Newton 3 fully exploit multiprossesor systems.