In multithreaded C/C++, does malloc/new lock the heap when allocating memory

后端 未结 4 683
一整个雨季
一整个雨季 2020-12-24 07:27

I\'m curious as to whether there is a lock on memory allocation if two threads simultaneously request to allocate memory. I am using OpenMP to do multithreading, C++ code.

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-24 07:56

    Free store is a shared resource and must be synchronized. Allocation/deallocation is costly. If you are multithreading for performance, then frequent allocation/deallocation can become a bottleneck. As a general rule, avoid allocation/deallocation inside tight loops. Another problem is false sharing.

提交回复
热议问题