In glibc, malloc
is implemented with arenas.
So, for example, it is possible that the memory first allocated by malloc
and later free
d in thread A can not be used by another call of malloc
in thread B, since thread A and B may be in different arenas, and different arenas maintain different heaps and free lists of memory.
When it comes to C++ (maybe also C++11 since C++11 has a new standard), is the story still the same?
Or different threads actually share the same segment of heap and free list of memory, and new
in one thread can allocate the memory first new
ed and later delete
d by another thread?
If the answer is implementation dependent, then the question is how are they implemented in the major C++ compilers, such as g++, MVC++, icc?
EDIT
I think this question is valid in the sense that sometimes you launch many threads and in each thread you dynamically allocate/deallocate a big chunk of memory for a large number of objects, and you don't want the memory usage by your application to go ridiculously high.