Re: Reentrant functions and memory allocation



CBFalconer wrote:
fmassei@xxxxxxxxx wrote:
I'm trying to put some old code in a portable, cross-platform
library and I'm facing a big problem: is there a standard,
platform-independent way to write a reentrant function that
allocate dynamic memory? In other words: is it safe to use
malloc() or free() in a thread-safe function?

No. Because the malloc system will be operating on it's own memory
areas, and is not re-entrant. You have to ensure that all accesses
to the malloc package are done through appropriate guards.

<off-topic>

For POSIX threads, malloc() et al. already contain whatever
guards are required, and you need provide no further protection
when calling them.

It's because of answers of this kind -- sort of right but
mostly not -- that it's been suggested you seek advice not here,
but on a forum where threading experts hang out.

</off-topic>

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxxx
.



Relevant Pages