In multithreaded C/C++, does malloc/new lock the heap when allocating memory

Janik Zikovsky picture Janik Zikovsky · Dec 24, 2010 · Viewed 10.2k times · Source

I'm curious as to whether there is a lock on memory allocation if two threads simultaneously request to allocate memory. I am using OpenMP to do multithreading, C++ code.

OS's: mostly linux, but would like to know for Windows and Mac as well.

Answer

Kirill V. Lyadvinsky picture Kirill V. Lyadvinsky · Dec 24, 2010

There could be improvements in certain implementations, such as creating a thread-specific cache (in this case allocations of small blocks will be lock-free). For instance, this from Google. But in general, yes, there is a lock on memory allocations.