Moderators: Sascha Willems, walaber
Gianluca wrote:Hello,
But If I try to run more than one NewtonWorld instances on various thread (with SetThreadCount(1) ), the application will crash in some Newton function... some time CreateBody or CreateCollision or dgSortArray::Sort, dgBodyMasterList::RemoveConstraint().
Gianluca wrote: -1) when I run one NewtonWorld using more than one thread (i.e. SetThreadCounts(4)) I don't have problems..
but
-2) when I run more than one NewtonWorld concurrently even if I set ThreadCounts(1) ... the application will crash...
so,
Gianluca wrote:Is the pool a global variable shared amongs all NewtonWorld created ??
If yes, one solution it could be to create differents pools for different worlds... right ?!?!
#include <pthread.h>
#include "Newton.h"
#include "dVector.h"
#include <iostream>
using namespace std;
//--- number of threads per world
#define NEWTON_THREADS 1
//--- number of worlds to create
#define NEWTON_WORLDS 4
#define NUM_STEPS 500
void applyForceAndTorqueHandler(const NewtonBody* body, dFloat timestep, int) {
NewtonWorld* world = NewtonBodyGetWorld( body );
NewtonWorldCriticalSectionLock( world );
// apply the gravity force
dVector force( 0.0f, 0.0f, -9.8 );
NewtonBodySetForce( body, &force[0] );
NewtonWorldCriticalSectionUnlock( world );
}
void* newtonSimul( void* ) {
NewtonWorld* world = NewtonCreate( NULL, NULL );
dVector minP(-20,-20,-20), maxP(20,20,20);
NewtonSetWorldSize( world, &minP[0], &maxP[0] );
NewtonSetMinimumFrameRate( world, 100 );
NewtonSetSolverModel( world, 0 );
NewtonSetFrictionModel( world, 0 );
NewtonSetThreadsCount( world, NEWTON_THREADS );
cout << "World Created" << endl;;
//--- ForEver
while( 1 ) {
//--- creating objects
NewtonBody* bodies[30];
NewtonCollision* collisions[30];
for( int i=0; i<30; i++ ) {
collisions[i] = NewtonCreateBox( world, 1, 2, 1, 1, 0 );
bodies[i] = NewtonCreateBody( world, collisions[i] );
NewtonBodySetAutoSleep( bodies[i], 0 );
dVector inertia;
dVector centre( 0, 0, 0 );
NewtonConvexCollisionCalculateInertialMatrix( collisions[i], &inertia[0], ¢re[0] );
NewtonBodySetMassMatrix( bodies[i], 1.0, inertia[0], inertia[1], inertia[2] );
NewtonBodySetLinearDamping( bodies[i], 0.0 );
dVector zero(0,0,0);
NewtonBodySetAngularDamping( bodies[i], &zero[0] );
NewtonBodySetAutoSleep( bodies[i], 0 );
NewtonBodySetForceAndTorqueCallback( bodies[i], applyForceAndTorqueHandler );
}
cout << "Objects Created" << endl;;
//--- simulating a bit
cout << "Simulating" << endl;
for( int i=0; i<500; i++ ) {
NewtonUpdate( world, 0.01 );
}
//--- Destroying objects
for( int i=0; i<30; i++ ) {
NewtonDestroyBody( world, bodies[i] );
NewtonReleaseCollision( world, collisions[i] );
}
cout << "Objects Destroyed" << endl;
}
return NULL;
}
int main( int argc, char* argv[] ) {
pthread_t threads[NEWTON_WORLDS];
for( int i=0; i<NEWTON_WORLDS; i++ ) {
pthread_create( threads+i, NULL, newtonSimul, NULL );
}
for( int i=0; i<NEWTON_WORLDS; i++ ) {
pthread_join( threads[i], NULL );
}
}
dgBroadPhaseCell* dgBroadPhaseLayer::FindCreate (dgInt32 x, dgInt32 z)
{
dgInt32 key;
dgTreeNode * node;
key = GetKey (x, z);
node = dgTree<dgBroadPhaseCell, dgUnsigned32>::Find (key);
if (!node) {
dgBroadPhaseCell cell;
cell.m_count = 0;
node = Insert(cell, key);
node->GetInfo().Init (m_layerIndex);
}
return &node->GetInfo();
}
#define DG_CLASS_ALLOCATOR_NEW inline void *operator new (size_t size) { return dgMalloc(size); }
#define DG_CLASS_ALLOCATOR_NEW_ARRAY inline void *operator new[] (size_t size) { return dgMalloc(size); }
#define DG_CLASS_ALLOCATOR_DELETE inline void operator delete (void *ptr) { dgFree(ptr); }
#define DG_CLASS_ALLOCATOR_DELETE_ARRAY inline void operator delete[] (void *ptr) { dgFree(ptr); }
#define DG_CLASS_ALLOCATOR \
DG_CLASS_ALLOCATOR_NEW \
DG_CLASS_ALLOCATOR_DELETE \
DG_CLASS_ALLOCATOR_NEW_ARRAY \
DG_CLASS_ALLOCATOR_DELETE_ARRAY
template<class T>
class dgTemplateVector
{
public:
DG_CLASS_ALLOCATOR
dgTemplateVector ();
dgTemplateVector (const T *ptr);
dgTemplateVector (T m_x, T m_y, T m_z, T m_w);
dgTemplateVector Scale (T s) const;
T& operator[] (dgInt32 i);
const T& operator[] (dgInt32 i) const;
dgTemplateVector operator+ (const dgTemplateVector &A) const;
dgTemplateVector operator- (const dgTemplateVector &A) const;
dgTemplateVector &operator+= (const dgTemplateVector &A);
dgTemplateVector &operator-= (const dgTemplateVector &A);
// return dot product
T operator% (const dgTemplateVector &A) const;
// return cross product
dgTemplateVector operator* (const dgTemplateVector &B) const;
// component wise multiplication
dgTemplateVector CompProduct (const dgTemplateVector &A) const;
T m_x;
T m_y;
T m_z;
T m_w;
};
[code]#define DG_CLASS_ALLOCATOR_NEW(allocator) inline void *operator new (size_t size, void* allocator) { return dgMalloc(size, allocator); }
#define DG_CLASS_ALLOCATOR_NEW_ARRAY(allocator) inline void *operator new[] (size_t size, void* allocator) { return dgMalloc(size, allocator); }
#define DG_CLASS_ALLOCATOR_DELETE(allocator) inline void operator delete (void *ptr, void* allocator) { dgFree(ptr, allocator); }
#define DG_CLASS_ALLOCATOR_DELETE_ARRAY(allocator) inline void operator delete[] (void *ptr, void* allocator) { dgFree(ptr, allocator); }
#define DG_CLASS_ALLOCATOR(allocator) \
DG_CLASS_ALLOCATOR_NEW(allocator) \
DG_CLASS_ALLOCATOR_DELETE(allocator) \
DG_CLASS_ALLOCATOR_NEW_ARRAY(allocator) \
DG_CLASS_ALLOCATOR_DELETE_ARRAY(allocator)[/code]
void* dgApi dgMalloc (size_t size)
{
void *ptr;
ptr = NULL;
if (size) {
// ptr = dgInternalMemory::g_allocaror.Malloc (dgInt32 (size));
ptr = dgInternalMemory::g_allocaror.MallocLow (dgInt32 (size));
}
return ptr;
}
// general deletion allocation for all data in the library
void dgApi dgFree (void *ptr)
{
if (ptr) {
// dgInternalMemory::g_allocaror.Free (ptr);
dgInternalMemory::g_allocaror.FreeLow (ptr);
}
}
new (int size)
{
void *ptr;
size += (1<<granulatiry);
size >> granulatiry;
ptr = Pool[size]->Last;
Pool[size]->Last = Pool[size]->Last->Last;
return ptr;
}
// testMultithread.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define __restrict__ __restrict
#include <pthread.h>
#include "Newton.h"
#include "dVector.h"
#include <iostream>
using namespace std;
//--- number of threads per world
#define NEWTON_THREADS 3
//--- number of worlds to create
#define NEWTON_WORLDS 4
#define NUM_STEPS 500
void applyForceAndTorqueHandler(const NewtonBody* body, dFloat timestep, int)
{
NewtonWorld* world = NewtonBodyGetWorld( body );
// NewtonWorldCriticalSectionLock( world );
// apply the gravity force
dVector force( 0.0f, 0.0f, -9.8f );
NewtonBodySetForce( body, &force[0] );
// NewtonWorldCriticalSectionUnlock( world );
}
static int worldIndex;
void* newtonSimul( void* )
{
int mem;
NewtonWorld* world = NewtonCreate ();
mem = NewtonGetMemoryUsed();
dVector minP(-20,-20,-20), maxP(20,20,20);
NewtonSetWorldSize( world, &minP[0], &maxP[0] );
NewtonSetMinimumFrameRate( world, 100 );
NewtonSetSolverModel( world, 0 );
NewtonSetFrictionModel( world, 0 );
NewtonSetThreadsCount( world, NEWTON_THREADS );
int index = worldIndex ++;
cout << "World Created " << index << endl;;
//--- ForEver
while( 1 ) {
// for (int i = 0; i < 1; i ++) {
//--- creating objects
NewtonBody* bodies[30];
NewtonCollision* collisions[30];
for( int i=0; i<30; i++ ) {
collisions[i] = NewtonCreateBox( world, 1, 2, 1, 1, 0 );
bodies[i] = NewtonCreateBody( world, collisions[i] );
NewtonBodySetAutoSleep( bodies[i], 0 );
dVector inertia;
dVector centre( 0, 0, 0 );
NewtonConvexCollisionCalculateInertialMatrix( collisions[i], &inertia[0], ¢re[0] );
NewtonBodySetMassMatrix( bodies[i], 1.0, inertia[0], inertia[1], inertia[2] );
NewtonBodySetLinearDamping( bodies[i], 0.0 );
dVector zero(0,0,0);
NewtonBodySetAngularDamping( bodies[i], &zero[0] );
NewtonBodySetAutoSleep( bodies[i], 0 );
NewtonBodySetForceAndTorqueCallback( bodies[i], applyForceAndTorqueHandler );
}
cout << "Objects Created " << index << endl;;
//--- simulating a bit
cout << "Simulating" << endl;
for( int i=0; i<500; i++ ) {
NewtonUpdate( world, 0.01f );
}
mem = NewtonGetMemoryUsed();
//--- Destroying objects
for( int i=0; i<30; i++ ) {
NewtonDestroyBody( world, bodies[i] );
NewtonReleaseCollision( world, collisions[i] );
}
cout << "Objects Destroyed" << endl;
}
// NewtonDestroy (world);
return NULL;
}
int main( int argc, char* argv[] )
{
pthread_t threads[NEWTON_WORLDS];
NewtonSetMemorySystem (NULL, NULL);
for( int i=0; i<NEWTON_WORLDS; i++ ) {
pthread_create( threads+i, NULL, newtonSimul, NULL );
}
for( int i=0; i<NEWTON_WORLDS; i++ ) {
pthread_join( threads[i], NULL );
}
}
Users browsing this forum: No registered users and 1 guest