Question

I have a multithreaded application using boost::thread. For performance reasons, I'd like each thread to have an independent heap.

I can create a heap using HeapCreate() but am unclear how to hook this up to the CRT library so that new and malloc allocate memory on the created heap. How can this be done?

Was it helpful?

Solution

How can this be done?

It can't be done without completely replacing the entire memory allocator. For example the scalable memory manager Hoard does exactly that. But replacing the memory allocator is not for the faint hearted.

If you want to use per-thread heap with HeapCreate, and your allocation/deallocation code is reasonably contained, then you could simply call HeapAlloc and HeapFree explicitly in your thread code. However, I'd be surprised if this was as fast as the standard CRT allocator which performs well.

OTHER TIPS

Unfortunately, there is no safe and documented way to replace CRT heap. Hacks only.

For new/delete statements you could provide global operator new() and operator delete() that will allocate/deallocate memory in required heap according to current thread. Thread Local Storage (TLS) is usually used to store thread local heap handle.

For malloc/free there is no such documented way to replace. Most of hacks are described here.

You can provide a "global operator new", which you can use to check what thread you're in and return memory from the appropiate heap. You'll need to supply your own "global delete" as well, of course.

This will, of course, only "catch" cases where new and delete are actually used :-)

Another approach: use multiple DLLs, each identical in all but name. Load a different DLL per thread.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top