Q: Overloaded Operator New / Scalable Memory Allocator

Q: Overloaded Operator New / Scalable Memory Allocator

I heard that if the non-tbb memory allocator likemalloc or calloc is used, ALL the thread may easily get blocked if the allocatorare blocking to allocate memory, is this true?

If the above is true,does the same thing happen to the new allocator? Does the tbb overloaded operator new avoid blocking? To ensure the tbb overloaded operator new is used, what particular manner[like including a header] should we take?

2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Memory is a global resource, therefore it should be protected in multi-threaded environment. Some implementations of malloc etc. can use a global lock for such purpose, so the scenario you outlined is possible; but it would not be correct to say it is always true with malloc. Also, in worst case I think all but one thread could be blocked; I would expect the thread that was first to acquire the lock will proceed, and eventually release it allowing another thread to proceed. So the memory allocation could be serialized, but not completely deadlocked unless someone is wrong in malloc implementation.

The C++ new and delete operators usually redirect to malloc, or use the same underlying API as malloc does.This is not specified by the standard, but I would expect it to be true in most cases. To be sure, you need to look at the particular implementation of the C++ runtime you use.

In the TBB scalable memory allocator, each thread uses its own memory heap for object allocation. There is no global lock, the allocation is fast and in most cases does not require any lock to be acquired. However there is no guarantee of non-blocking behavior, as from time to time the allocator needs to access the global memory pool to request a new big piece of memory. Eventually, every allocator uses system calls such as mmap or VirtualAlloc to request memory from an operating system, which also somehow protects consistency.

TBB does not provide replacements of global new and delete operators. There is an example in the TBB book how you could make it though. The C++ standard poses some requirements on such replacement operators; in particular, it says that

"A C + + program shall provide at most one definition of a replaceable allocation or deallocation function."

Thus the usual way to implement replacement functions is to have a separate compilation unit (a source file) that contains definitions of these functions, and link this file into the final C++ program. Please also note that any particular class can overload new and delete operators used to create/delete objects of this class.

I hope this helps, and wil be glad to answer subsequent questions if any.

Leave a Comment

Please sign in to add a comment. Not a member? Join today