多线程堆管理 [英] Multithreaded Heap Management

查看:102
本文介绍了多线程堆管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C/C ++中,我可以在一个线程中分配内存,并在另一个线程中将其删除.但是,只要有人从堆请求内存,堆分配器就需要遍历堆以找到适当大小的可用区域.两个线程如何在不破坏堆的情况下有效地访问同一堆? (这是通过锁定堆完成的吗?)

In C/C++ I can allocate memory in one thread and delete it in another thread. Yet whenever one requests memory from the heap, the heap allocator needs to walk the heap to find a suitably sized free area. How can two threads access the same heap efficiently without corrupting the heap? (Is this done by locking the heap?)

推荐答案

通常,您不必担心内存分配器的线程安全性.所有标准内存分配器(即MacOS,Windows,Linux等随附的那些内存分配器)都是线程安全的.锁是提供线程安全性的标准方法,尽管可以编写仅使用原子操作而不使用锁的内存分配器.

In general, you do not need to worry about the thread-safety of your memory allocator. All standard memory allocators -- that is, those shipped with MacOS, Windows, Linux, etc. -- are thread-safe. Locks are a standard way of providing thread-safety, though it is possible to write a memory allocator that only uses atomic operations rather than locks.

现在,那些内存分配器是否 scale 是一个完全不同的问题;也就是说,它们的性能是否独立于执行内存操作的线程数?在大多数情况下,答案是否定的.它们要么变慢,要么会消耗很多 的内存.在两个维度(速度和空间)上的第一个可扩展分配器是 Hoard (其中我写); Mac OS X分配器的灵感来自于它-并在文档中引用了它-但Hoard更快.还有其他一些,包括Google的tcmalloc.

Now it is an entirely different question whether those memory allocators scale; that is, is their performance independent of the number of threads performing memory operations? In most cases, the answer is no; they either slow down or can consume a lot more memory. The first scalable allocator in both dimensions (speed and space) is Hoard (which I wrote); the Mac OS X allocator is inspired by it -- and cites it in the documentation -- but Hoard is faster. There are others, including Google's tcmalloc.

这篇关于多线程堆管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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