Alex Rivera | Logout

Multithreaded Heap Management

Asked 2010-01-28T21:42:11.767
12

In C/C++ I can allocate memory in one thread and delete it in another thread. Yet whenever one requests memory from the heap, the heap allocator needs to walk the heap to find a suitably sized free area. How can two threads access the same heap efficiently without corrupting the heap? (Is this done by locking the heap?)

Edit
Report

1 Answer

0

This will depend heavily on your platform/OS, but I believe this is generally OK on major sytems. C/C++ do not define threads, so by default I believe the answer is "heap is not protected", that you must have some sort of multithreaded protection for your heap access.

However, at least with linux and gcc, I believe that enabling -pthread will give you this protection automatically...

Additionally, here is another related question:

C++ new operator thread safety in linux and gcc 4

answered 2010-01-28T21:53:44.273

Your Answer