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?
Wrong. Reentrant means you can interrupt it and call it again before the previous incarnation ended. Imagine malloc looks like this:
What happens if it is interrupted in the middle, before unlocking and someone else calls
malloc
?That's a deadlock.
Wrong again. See the example above. Imagine the main program is doing a
malloc
and before the function actually ends your handler calls malloc.