在多线程C/C ++中,分配内存时malloc/new是否锁定堆 [英] In multithreaded C/C++, does malloc/new lock the heap when allocating memory

查看:332
本文介绍了在多线程C/C ++中,分配内存时malloc/new是否锁定堆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,如果两个线程同时请求分配内存,内存分配是否存在锁定.我正在使用OpenMP进行多线程C ++代码.

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.

操作系统:主要是linux,但也想了解Windows和Mac.

OS's: mostly linux, but would like to know for Windows and Mac as well.

推荐答案

在某些实现中可能会有改进,例如创建特定于线程的缓存(在这种情况下,小块的分配将是无锁的).例如,来自Google的.但是总的来说,是的,内存分配有锁.

There could be improvements in certain implementations, such as creating a thread-specific cache (in this case allocations of small blocks will be lock-free). For instance, this from Google. But in general, yes, there is a lock on memory allocations.

这篇关于在多线程C/C ++中,分配内存时malloc/new是否锁定堆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆