On Apr 27, 2005, at 4:30 AM, Jez White wrote:

Would the following function be thread safe (called at the same time from different C threads - the actual SV belongs to the same thread doing the calling)?

void someFunction (pTHX_ SV* someSV) {
 //do something with the SV here
}

Since you're passing the thread context (pTHX_) it should be safe from the interpreter point of view, regardless of whether you're using interpreter threads (which share a heap) or multiple interpreters (which have separate heaps).


What you do with the data from the SV must also be thread-safe.


While we're on the subject of thread safety - can anyone point me to documentation explaining the zen of memory allocation (malloc, safemalloc etc) under a threaded environment? As an example, is it save for two C threads (each with it's own perl interpreter) to call malloc (or safemalloc) at the same time? Or should I be protecting these calls via a mutex?

A quick google reveals several mailing list posts claiming that glibc's malloc is threadsafe, and at least one that says it's threadsafe if you compile with _REENTRANT defined.


Beware that perl.h #defines malloc differently depending on your perl's configuration, so if you want to make absolutely certain you're using libc malloc, you need to #undef malloc before calling it.

Discussion of PerlMem_malloc versus safemalloc versus safesysmalloc etc is out of my depth. Sorry.


-- Without treatment, a common cold will last about seven days. With treatment, it will last about a week. -- conventional wisdom



Reply via email to