How does malloc work in a multithreaded environment?
glibc 2.15 operates 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: /* arena_get() acquires an arena and locks the corresponding … Read more