多线程可以加速内存分配吗?

我正在使用 8 核处理器,并且正在使用 Boost 线程来运行大型程序.从逻辑上讲,程序可以分成多个组,每个组由一个线程运行.在每个组中,某些类总共调用了 10000 次new"运算符.Rational Quantify 显示新"内存分配在程序运行时占用了最长的处理时间,并且正在减慢整个程序.

I'm working with an 8 core processor, and am using Boost threads to run a large program. Logically, the program can be split into groups, where each group is run by a thread. Inside each group, some classes invoke the 'new' operator a total of 10000 times. Rational Quantify shows that the 'new' memory allocation is taking up the maximum processing time when the program runs, and is slowing down the entire program.

我可以加快系统速度的一种方法是在每个组"内使用线程,以便 10000 次内存分配可以并行发生.

One way I can speed up the system could be to use threads inside each 'group', so that the 10000 memory allocations can happen in parallel.

我不清楚这里将如何管理内存分配.操作系统调度器真的能够并行分配内存吗?

I'm unclear of how the memory allocation will be managed here. Will the OS scheduler really be able to allocate memory in parallel?

推荐答案

内存的动态分配使用应用程序/模块/进程(但不是线程)的堆.堆一次只能处理一个分配请求.如果您尝试在并行"线程中分配内存,它们将由堆按适当的顺序处理.你不会得到这样的行为:一个线程正在等待获取它的内存,而另一个线程可以请求一些,而第三个线程正在获取一些.线程必须在队列中排队以获取它们的内存块.

Dynamic allocation of memory uses the heap of the application/module/process (but not thread). The heap can only handle one allocation request at a time. If you try to allocate memory in "parallel" threads, they will be handled in due order by the heap. You will not get a behaviour like: one thread is waiting to get its memory while another can ask for some, while a third one is getting some. The threads will have to line-up in queue to get their chunk of memory.

你需要的是一个堆池.使用当前不忙的堆来分配内存.但是,您必须在此变量的整个生命周期中保持警惕,以免它在另一个堆上被取消分配(这会导致崩溃).

What you would need is a pool of heaps. Use whichever heap is not busy at the moment to allocate the memory. But then, you have to watch out throughout the life of this variable such that it does not get de-allocated on another heap (that would cause a crash).

我知道 Win32 API 具有 GetProcessHeap()、CreateHeap()、HeapAlloc() 和 HeapFree() 等函数,它们允许您创建新堆并从特定堆 HANDLE 分配/释放内存.我不知道其他操作系统中的等效项(我已经寻找过它们,但无济于事).

I know that Win32 API has functions such as GetProcessHeap(), CreateHeap(), HeapAlloc() and HeapFree(), that allow you to create a new heap and allocate/deallocate memory from a specific heap HANDLE. I don't know of an equivalence in other operating systems (I have looked for them, but to no avail).

当然,您应该尽量避免频繁进行动态分配.但是如果你不能,你可能会考虑(为了可移植性)创建你自己的堆"类(不必是堆本身,只是一个非常有效的分配器),它可以管理大块内存并且肯定一个智能指针类,它将保存对它来自的堆的引用.这将使您能够使用多个堆(确保它们是线程安全的).

You should, of course, try to avoid doing frequent dynamic allocations. But if you can't, you might consider (for portability) to create your own "heap" class (doesn't have to be a heap per se, just a very efficient allocator) that can manage a large chunk of memory and surely a smart pointer class that would hold a reference to the heap from which it came. This would enable you to use multiple heaps (make sure they are thread-safe).

相关文章