c++multithreadingcontention

thread contention on dynamic memory allocation


I have just learnt that in C language malloc function comes with the issue of thread contention when used in a multi-threaded applications.

In C++ does operator new suffer from the same problem? If yes what tecnhique can I use to avoid this that sounds like a big penalty in the application performance?


Solution

  • That "issue" of thread contention really depends on the implementation. Some of the implementations of malloc in common use were not originally designed with multithreading in mind. But a malloc implementation designed for multithreaded applications shouldn't suffer from contention in normal circumstances.

    As an example of a malloc implementation designed with multithreading in mind, look at jemalloc.