paperjam
paperjam

Reputation: 8528

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?

Upvotes: 2

Views: 1583

Answers (4)

paperjam
paperjam

Reputation: 8528

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

Upvotes: 0

Christian Stieber
Christian Stieber

Reputation: 12496

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 :-)

Upvotes: 2

Rost
Rost

Reputation: 9089

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.

Upvotes: 2

David Heffernan
David Heffernan

Reputation: 613003

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.

Upvotes: 3

Related Questions