c++windowsvisual-c++heap-memorycrt

C++ / Windows: HeapAlloc() for contention-free per-thread heaps


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?


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.