Using Chipmunk in Multiple Threads

Official forum for the Chipmunk2D Physics Library.
Post Reply
Beta Carotene
Posts: 123
Joined: Sat Aug 04, 2012 6:34 pm
Contact:

Using Chipmunk in Multiple Threads

Post by Beta Carotene »

So... few things:

A) I am not entirely familiar with how chipmunk is written
B) I am not entirely familiar with multi-threading (ok who am i kidding, I've never done it =P )

Basically, my question is very simple, but I don't know if it has a simple answer: is it safe to run chipmunk on multiple threads?

So, in other words, just having one space and a set of bodies/shapes in each thread and simulating them concurrently. Are there issues I should be aware of? Is Chipmunk even thread safe?

EDIT: Ok, great example right here

char*
cpvstr(const cpVect v)
{
static char str[256];
sprintf(str, "(% .3f, % .3f)", v.x, v.y);
return str;
}

I'm no master of multi-threading but I KNOW this function is not thread safe. Two threads accessing at the same time will both modify the same instance of the character array.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Using Chipmunk in Multiple Threads

Post by slembcke »

Chipmunk doesn't have any global shared state. You can safely run separate Chipmunk spaces in different threads.

It's not thread safe however. If you want to access a single space from multiple threads, you'll need to use mutexes.

Good luck. ;) Threads are fun, but also often mindbogglingly difficult to debug when something goes wrong. I like the saying, "If you think you have a problem that can be solved with threads, now you have two problems".
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Beta Carotene
Posts: 123
Joined: Sat Aug 04, 2012 6:34 pm
Contact:

Re: Using Chipmunk in Multiple Threads

Post by Beta Carotene »

But that function I posted above, that's not exactly a globally shared state, but its using a static local variable. If you attempted to use that function in multiple threads it may not work. Or is there something I'm not understanding about this?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Using Chipmunk in Multiple Threads

Post by slembcke »

A static local variable may not be a global variable, but it is globally shared state.

/me facepalm

I was about to say that the only exception I know of is the cpvstr() function which does something similar to the function you posted. I didn't actually read it close enough to see that it was my function. *derp* -_- It's really only meant for debugging purposes anyway.

Other than that, everything else in Chipmunk operates on specific instances, and none of them share data unless you set up the relationship (i.e. adding a body to a space or adding a shape to a body, etc).
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Beta Carotene
Posts: 123
Joined: Sat Aug 04, 2012 6:34 pm
Contact:

Re: Using Chipmunk in Multiple Threads

Post by Beta Carotene »

I did a search for the word "static" through my entire program, including all the chipmunk files and, besides that one line, I found nothing outside of static methods, which are obviously fine. I changed the function to just use a locally declared array of characters just in case (If I don't use it much it shouldn't hurt right? =P ).

Alright, this is all re-assuring, so thanks.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Using Chipmunk in Multiple Threads

Post by slembcke »

Well, if you change it to use a local array of chars, then it will return stack garbage. The reason why it uses a static local is so that the array pointer stays valid after the function returns. Honestly a better thing to do would be just to output the x/y in your own print statements:

Code: Select all

printf("Woo a vector: (%5.2f, %5.2f)\n", v.x, v.y)
Then you can print more than one vector without having to split your print statements. I personally stopped using cpvstr() a long time ago. I just never deprecated or removed it from the API.

An even better thing to do would be to use a language where you don't have to do crappy C style I/O at all. ;)
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 17 guests