tags:

views:

3609

answers:

6

Is malloc re-entrant?

A: 

No, it is not.

http://coding.derkeiler.com/Archive/C_CPP/comp.lang.c/2007-07/msg02658.html

McWafflestix
As stated at http://groups.google.com/group/comp.lang.c.moderated/browse_thread/thread/2431a99b9bdcef11/ea800579e40f7fa4, this is not strictly correct. malloc is neither guaranteed to be, nor guaranteed not be, thread-safe. It is implementation-dependent.
Matthew Flaschen
Safety is not partial; if an implementation may not be safe, my interpretation is that it is not safe. Poster did not specify a platform; therefore, no guarantee of safety can be given.
McWafflestix
Any given implementation either is or isn't safe. To say that an unknown implementation is unsafe is no more true than saying that it is safe. The correct answer is that it's implementation-dependent.
ChrisW
@ChrisW: since the OP did not specify the implementation, and since safety is an absolute condition (it can be broken with only one exception; only one unsafe action can make something "unsafe"), I decided to answer that it was unsafe. The original poster can determine for him/her self what is the correct answer.
McWafflestix
+6  A: 

I read somewhere that if you compile with -pthreads, malloc becomes thread safe. I´m pretty sure its implementation dependant though, since malloc is ANSI C and threads are not.

If we are talking gcc:

Compile and link with -pthreads and malloc() will be thread-safe, on x86 and AMD64.

http://groups.google.com/group/comp.lang.c.moderated/browse_thread/thread/2431a99b9bdcef11/ea800579e40f7fa4

Another opinion, more insightful

{malloc, calloc, realloc, free, posix_memalign} of glibc-2.2+ are thread safe

http://linux.derkeiler.com/Newsgroups/comp.os.linux.development.apps/2005-07/0323.html

Tom
+1  A: 

It depends on which implementation of the C runtime library you're using. If you're using MSVC for example then there's a compiler option which lets you specify which version of the library you want to build with (i.e. a run-time library that supports multi-threading by being tread-safe, or not).

ChrisW
+1  A: 

No, it is not thread-safe. There may actually be a malloc_lock() and malloc_unlock() function available in your C library. I know that these exist for the Newlib library. I had to use this to implement a mutex for my processor, which is multi-threaded in hardware.

sybreon
Some implementations of malloc are thread-safe.
ChrisW
+7  A: 

Question: "is malloc reentrant"?
Answer: no, it is not. Here is one definition of what makes a routine reentrant.

None of the common versions of malloc allow you to reenter it (e.g. from signal handler). Note that a reentrant routine may not use locks, and almost all malloc versions in existence do use locks (which makes them thread-safe), or global/static variables (which makes them thread-unsafe and non-reentrant).

All the answers so far answer "is malloc thread-safe?", which is entirely different question. To that question the answer is it depends on your runtime library, and possibly on the compiler flags you use. On any modern UNIX you'll get a thread-safe malloc by default. On Windows, use /MT, /MTd, /MD or /MDd flags to get thread-safe runtime library.

Employed Russian
A: 

Here is an excerpt from malloc.c of glibc :

Thread-safety: thread-safe unless NO_THREADS is defined

assuming NO_THREADS is not defined by default, malloc is thread safe at least on linux.

shahkhas