Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

malloc() is non-reentrant but thread-safe? [duplicate]

Possible Duplicate:
Malloc thread-safe?

I am not a bit confused while I am reading "The Linux Programming Interface".

From the book it says that malloc is non-reentrant since it manipulates the global linked list data structure but is made thread-safe by using mutex.

I am a bit confused about this: since it's thread-safe with using mutex and thus can be invoked by more than one threads at the same time, why it isn't a reentrant function? (if we say that reentrant means that it can be invoked by more than one caller at the same time)

Another question is that, since malloc is thread-safe, can we put it in a signal handler? I think the answer is yes but I am not sure since according to this book, it says that only a reentrant or async-signal-safe function can be put in the signal handler.

Can anyone explain this to me?

like image 969
kai Avatar asked Jan 15 '12 12:01

kai


People also ask

Why malloc () is not a reentrant function?

On most systems, malloc and free are not reentrant, because they use a static data structure which records what memory blocks are free. As a result, no library functions that allocate or free memory are reentrant. This includes functions that allocate space to store a result.

Does reentrant mean thread-safe?

An operation is “thread-safe” if it can be performed from multiple threads safely, even if the calls happen simultaneously on multiple threads. An operation is re-entrant if it can be performed while the operation is already in progress (perhaps in another context).

Is malloc free thread-safe?

By default, new/delete are often not thread safe in a FreeRTOS app because the basic malloc and free are not thread safe. Some embedded libraries have hooks that can be defined to make these functions thread safe.

Is malloc thread-safe in Linux?

None of the common versions of malloc allow you to re-enter it (e.g. from a 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).


2 Answers

if we say that reentrant means that it can be invoked by more than one caller at the same time

Wrong. Reentrant means you can interrupt it and call it again before the previous incarnation ended. Imagine malloc looks like this:

lock(mutex);

/* Stuff. */

unlock(mutex):

What happens if it is interrupted in the middle, before unlocking and someone else calls malloc ?

  • The first context cannot continue until the second one is done
  • The second context blocks on the mutex and cannot continue until the first unlocks the mutex

That's a deadlock.

Another question is that, since malloc is thread-safe, can we put it in a signal handler? I think the answer is yes

Wrong again. See the example above. Imagine the main program is doing a malloc and before the function actually ends your handler calls malloc.

like image 137
cnicutar Avatar answered Oct 26 '22 23:10

cnicutar


Reentrancy and thread-safety are two different concepts. A reentrant function can be non-thread -safe and a thread-safe function can be non-reentrant.

Library functions in C are not guaranteed to be reentrant and only reentrant functions can be called from signal handlers.

like image 24
ouah Avatar answered Oct 27 '22 00:10

ouah