26

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.

Janik Zikovsky
  • 3,086
  • 7
  • 26
  • 34
  • 2
    Are you afraid two threads will be allocated the same memory? If so you need not worry. If malloc wasn't thread safe, you would not be the first to discover it. If you question is -how- does malloc achieve thread safeness, thats implementation dependent. – EnabrenTane Dec 24 '10 at 05:43
  • My question was related more to performance when allocating in parallel threads. – Janik Zikovsky Dec 26 '10 at 15:31

4 Answers4

15

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.

Shog9
  • 156,901
  • 35
  • 231
  • 235
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
7

By default Windows locks the heap when you use the Win API heap functions.

You can control the locking at least at the time of heap creation. Different compilers and C runtimes do different things with the malloc/free family. For example, the SmartHeap API at one point created one heap per thread and therefore needed no locking. There were also config options to turn that behavior on and off.

At one point in the early/mid '90s the Borland Windows and OS/2 compilers explicitly turned off Heap locking (a premature optimization bug) until multiple threads were launched with beginthread. Many many people tried to spawn threads with an OS API call and then were surprised when the heap corrupted itself all to hell...

JimR
  • 15,513
  • 2
  • 20
  • 26
5

http://en.wikipedia.org/wiki/Malloc

Modern malloc implementations try to be as lock-free as possible by keeping separate "arenas" for each thread.

MK.
  • 33,605
  • 18
  • 74
  • 111
  • 5
    Wikipedia is not an [authoritative source](http://chronicle.com/blogs/wiredcampus/wikipedia-founder-discourages-academic-use-of-his-creation/2305): By all means use it as a starting point for learning and finding authoritative sources. But it should not be quoted as it is not an authoritative source. – Martin York Dec 24 '10 at 06:28
  • 18
    The link is useful, regardless of whether or not it is authoritative. We're not writing scholarly papers here. – Brent Bradburn Jan 31 '11 at 19:18
5

Free store is a shared resource and must be synchronized. Allocation/deallocation is costly. If you are multithreading for performance, then frequent allocation/deallocation can become a bottleneck. As a general rule, avoid allocation/deallocation inside tight loops. Another problem is false sharing.

watson1180
  • 2,015
  • 1
  • 18
  • 24