Is the malloc()
function re-entrant?
Is malloc thread-safe
c++mallocmultithreadingthread-safety
Related Question
- Do I cast the result of malloc
- What REALLY happens when you don’t free after malloc
- C# – How to update the GUI from another thread
- Difference between malloc and calloc
- Php – thread safe or non-thread safe in PHP
- C++ – the “–>” operator in C/C++
- Sqlite – Improve INSERT-per-second performance of SQLite
- Android “Only the original thread that created a view hierarchy can touch its views.”
Best Solution
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 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).
All the answers so far answer "is malloc thread-safe?", which is an 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.