A place to discuss everything related to Newton Dynamics.
Moderators: Sascha Willems, walaber
by ToniMachado » Tue May 26, 2009 10:26 am
I'm using Newton Game Dynamics with OpenGL to implement my own robotics simulator for my PhD.
My robot has 11 distance sensors. To implement theses distance sensors, I using NewtonWorldRayCast function. This function requires a static function that implements the algorithm which returns the distance value.
Each robot runs under a specific thread, and in this thread I need to read the distances to each possible obstacle.
My problem is when I insert, for example 4 robots, this 4 robots are trying to read its distances "at the same time". To synchronize theses thread I'm using mutex, but the distances are given by an implementation of a static function because the NewtonWorldRayCast only accepts static function as callback.
So, the problem is the thread of each robot is locked when other robot is getting a distance by any distance sensor.
Could someone help me to solve my big problem?
Thanks for your attention,
Toni Machado
-
ToniMachado
-
- Posts: 14
- Joined: Thu Jan 29, 2009 11:22 am
by JernejL » Tue May 26, 2009 12:10 pm
The raycast function has a userdata pointer that you can use to distinguish which distance sensor hit something.
-

JernejL
-
- Posts: 1587
- Joined: Mon Dec 06, 2004 2:00 pm
- Location: Slovenia
-
by ToniMachado » Tue May 26, 2009 12:58 pm
Yes, I know that.
But,
My problem is that all advantage on the use the threads lost, because in the example that I wrote in my post, I have 4 robot, i.e., 4 thread trying to access 11 times on each threats run to the same function, and to guaranty synchronism I use mutex.
Thanks,
Toni Machado
-
ToniMachado
-
- Posts: 14
- Joined: Thu Jan 29, 2009 11:22 am
by ToniMachado » Tue May 26, 2009 1:19 pm
What I need is to call a non-static function as a callback funtion by NewtonWorldRayCast function.
Thanks in advance,
Toni Machado
-
ToniMachado
-
- Posts: 14
- Joined: Thu Jan 29, 2009 11:22 am
by Julio Jerez » Tue May 26, 2009 1:41 pm
It is very easy,
Like Delfi say the ray casts take a parameter to user data.
If you assign data local to the robot, then in the call back you only need to do is tread safe when the you are going to write to data that is no local to the robot.
You do not need to use any Murex stuff, Newton provide support for very light weigh trade safe operations
- Code: Select all
static dFloat RayCastPlacement (const NewtonBody* body, const dFloat* normal, int collisionID, void* userData, dFloat intersetParam)
{
Newton* world;
// do all the claculation tah do no write to memory, and collect teh information in local variables
MyLocalData data;
// if I nee dto write to some non local data do it here
worlk = NewtonBodyGetWorld();
NewtonWorldCriticalSectionLock (worlk);
// do teh writing here
NewtonWorldCriticalSectionUnlock (newtonWorld);
return
}
-
Julio Jerez
- Moderator

-
- Posts: 12426
- Joined: Sun Sep 14, 2003 2:18 pm
- Location: Los Angeles
-
by ToniMachado » Wed May 27, 2009 6:31 am
Thanks Julio Jerez for your post,
But with your possible solution, I continue with the same bottleneck, because I only can read one distance sensor at each time.
When I have two, three, ..., N robots, I have too two, three, ..., N threads running and trying to access to the same funtion, and the power of threads is lost.
What you was suggested, I think it is the same of using mutex to thread safe.
I'm angry with this my problem...
I'm waitting for other possible aid.
Thanks,
Toni Machado
-
ToniMachado
-
- Posts: 14
- Joined: Thu Jan 29, 2009 11:22 am
by Julio Jerez » Wed May 27, 2009 8:15 am
What I sugeted is very different than a mutex, believe me I spent years figuring that out.
despite all the mombo jumbo put out there writting multytreaded code that can actually be more efficent than single treade, is not eassy, 99% of the cases you get a net loss in performance.
this is because thread switching is a extremetly expensive operation in windows, it was designed for application level multitasking, not for fine granularity tasking.
Newton for that reason uses a thread pool, that do not create treads, mutex, or semphores, is uses a set of thread that are always working but that are sleeping when they are not dowing work. Beleive ot not this is faster.
the function I gave you are spin locks, no mutexs.
also if your ray cast function do not write to shared memory, you do not need to use the locks, only when you write to memory that is not local to the object who own the thread you need to do it under a Critical Section.
you should get amost 100% efficiency with ray casting.
-
Julio Jerez
- Moderator

-
- Posts: 12426
- Joined: Sun Sep 14, 2003 2:18 pm
- Location: Los Angeles
-
by ToniMachado » Wed May 27, 2009 10:13 am
Thanks Julio Jerez for your post,
I had not understood what you said. After read again, I understood it.
Thanks a lot for your help.
Thanks to Delfi too.
Best regards,
Toni Machado.
-
ToniMachado
-
- Posts: 14
- Joined: Thu Jan 29, 2009 11:22 am
Return to General Discussion
Who is online
Users browsing this forum: No registered users and 3 guests