Does the typical malloc (for x86-64 platform and Linux OS) naively lock a mutex at the beginning and release it when done, or does it lock a mutex in a more clever way at a finer level, so that lock contention is reduced for concurrent calls? If it indeed does it the second way, how does it do it?
Does the typical malloc (for x86-64 platform and Linux OS) naively lock a mutex
Share
glibc 2.15operates multiple allocation arenas. Each arena has its own lock. When a thread needs to allocate memory,malloc()picks an arena, locks it, and allocates memory from it.The mechanism for choosing an arena is somewhat elaborate and is aimed at reducing lock contention:
With this in mind,
malloc()basically looks like this (edited for brevity):This allocator is called
ptmalloc. It is based on earlier work by Doug Lea, and is maintained by Wolfram Gloger.