多线程能加速内存分配吗? [英] Can multithreading speed up memory allocation?

查看:332
本文介绍了多线程能加速内存分配吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个8核处理器,我使用Boost线程来运行一个大程序。
逻辑上,程序可以分成组,每个组由一个线程运行。
在每个组内,一些类总共调用new运算符10000次。
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?

推荐答案

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

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).

I知道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).

这篇关于多线程能加速内存分配吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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