Linux 线程是否有不同的堆?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1665419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do threads have a distinct heap?
提问by
As far as I know each thread gets a distinct stack when the thread is created by the operating system. I wonder if each thread has a heap distinct to itself also?
据我所知,当操作系统创建线程时,每个线程都会获得一个不同的堆栈。我想知道每个线程是否也有一个与自身不同的堆?
采纳答案by brianegge
No. All threads share a common heap.
否。所有线程共享一个公共堆。
Each thread has a private stack, which it can quickly add and remove items from. This makes stack based memory fast, but if you use too much stack memory, as occurs in infinite recursion, you will get a stack overflow.
每个线程都有一个私有堆栈,它可以在其中快速添加和删除项目。这使得基于堆栈的内存速度很快,但是如果您使用过多的堆栈内存,就像在无限递归中发生的那样,您将获得堆栈溢出。
Since all threads share the same heap, access to the allocator/deallocator must be synchronized. There are various methods and libraries for avoiding allocator contention.
由于所有线程共享同一个堆,因此必须同步访问分配器/释放器。有多种方法和库可以避免分配器争用。
Some languages allow you to create private pools of memory, or individual heaps, which you can assign to a single thread.
某些语言允许您创建私有内存池或单个堆,您可以将其分配给单个线程。
回答by Ken Bloom
Typically, threads share the heap and other resources, however there are thread-like constructions that don't. Among these thread-like constructions are Erlang's lightweight processes, and UNIX's full-on processes (created with a call to fork()
). You might also be working on multi-machine concurrency, in which case your inter-thread communication options are considerably more limited.
通常,线程共享堆和其他资源,但也有类似线程的结构不共享。在这些类似线程的结构中,有 Erlang 的轻量级进程和 UNIX 的完整进程(通过调用 来创建fork()
)。您可能还在处理多机并发,在这种情况下,您的线程间通信选项会受到相当多的限制。
回答by R Samuel Klatchko
By default, C has only a single heap.
默认情况下,C 只有一个堆。
That said, some allocators that are thread aware will partition the heap so that each thread has it's own area to allocate from. The idea is that this should make the heap scale better.
也就是说,一些线程感知的分配器将对堆进行分区,以便每个线程都有自己的分配区域。这个想法是这应该使堆扩展更好。
One example of such a heap is Hoard.
这种堆的一个例子是Hoard。
回答by tsalter
Each thread has its own stack and call stack.
每个线程都有自己的堆栈和调用堆栈。
Each thread shares the same heap.
每个线程共享同一个堆。
回答by Meinersbur
Generally speaking, all threads use the same address space and therefore usually have just one heap.
一般来说,所有线程使用相同的地址空间,因此通常只有一个堆。
However, it can be a bit more complicated. You might be looking for Thread Local Storage(TLS), but it stores single values only.
但是,它可能会更复杂一些。您可能正在寻找线程本地存储(TLS),但它仅存储单个值。
Windows-Specific: TLS-space can be allocated using TlsAllocand freed using TlsFree(Overview here). Again, it's not a heap, just DWORDs.
Windows 特定:可以使用TlsAlloc分配 TLS 空间并使用TlsFree释放(此处概述)。同样,它不是堆,只是 DWORD。
Strangely, Windows support multiple Heapsper process. One can store the Heap's handle in TLS. Then you would have something like a "Thread-Local Heap". However, just the handle is not known to the other threads, they still can access its memory using pointers as it's still the same address space.
奇怪的是,Windows 支持每个进程多个堆。可以将堆的句柄存储在 TLS 中。然后你会有一个类似“线程本地堆”的东西。然而,只是句柄不为其他线程所知,他们仍然可以使用指针访问其内存,因为它仍然是相同的地址空间。
EDIT: Some memory allocators (specifically jemallocon FreeBSD) use TLS to assign "arenas" to threads. This is done to optimize allocation for multiple cores by reducing synchronization overhead.
编辑:一些内存分配器(特别是 FreeBSD 上的jemalloc)使用 TLS 将“ arenas”分配给线程。这样做是为了通过减少同步开销来优化多核的分配。
回答by Srikumar
Depends on the OS. The standard c runtime on windows and unices uses a shared heap across threads. This means locking every malloc/free.
取决于操作系统。windows 和 unices 上的标准 c 运行时使用跨线程共享堆。这意味着锁定每个 malloc/free。
On Symbian, for example, each thread comes with its own heap, although threads can share pointers to data allocated in any heap. Symbian's design is better in my opinion since it not only eliminates the need for locking during alloc/free, but also encourages clean specification of data ownership among threads. Also in that case when a thread dies, it takes all the objects it allocated along with it - i.e. it cannot leak objects that it has allocated, which is an important property to have in mobile devices with constrained memory.
例如,在 Symbian 上,每个线程都有自己的堆,尽管线程可以共享指向在任何堆中分配的数据的指针。在我看来,Symbian 的设计更好,因为它不仅消除了在 alloc/free 期间锁定的需要,而且还鼓励线程之间数据所有权的清晰规范。同样在这种情况下,当一个线程死亡时,它会带走它分配的所有对象 - 即它不能泄漏它已经分配的对象,这是具有受限内存的移动设备的一个重要属性。
Erlang also follows a similar design where a "process" acts as a unit of garbage collection. All data is communicated between processes by copying, except for binary blobs which are reference counted (I think).
Erlang 也遵循类似的设计,其中“进程”充当垃圾收集单元。所有数据都通过复制在进程之间进行通信,除了引用计数的二进制 blob(我认为)。
回答by VladV
It depends on what exactly you mean when saying "heap".
这取决于您在说“堆”时的确切含义。
All threads share the address space, so heap-allocated objects are accessible from all threads. Technically, stacks are shared as well in this sense, i.e. nothing prevents you from accessing other thread's stack (though it would almost never make any sense to do so).
所有线程共享地址空间,因此所有线程都可以访问堆分配的对象。从技术上讲,从这个意义上讲,堆栈也是共享的,即没有什么可以阻止您访问其他线程的堆栈(尽管这样做几乎没有任何意义)。
On the other hand, there are heap structuresused to allocate memory. That is where all the bookkeeping for heap memory allocation is done. These structures are sophisticatedly organized to minimize contention between the threads - so some threads might share a heap structure (an arena), and some might use distinct arenas.
See the following thread for an excellent explanation of the details: How does malloc work in a multithreaded environment?
另一方面,还有用于分配内存的堆结构。这就是堆内存分配的所有簿记完成的地方。这些结构经过精心组织,以最大限度地减少线程之间的争用——因此某些线程可能共享一个堆结构(一个区域),而某些线程可能使用不同的区域。
有关详细信息的出色解释,请参阅以下线程:malloc 在多线程环境中如何工作?
回答by Noreddine -Kessa
On FreeRTOS Operating system, tasks(threads) share the same heap but each one of them has its own stack. This comes in very handy when dealing with low power low RAM architectures,because the same pool of memory can be accessed/shared by several threads, but this comes with a small catch , the developer needs to keep in mind that a mechanism for synchronizing malloc and free is needed, that is why it is necessary to use some type of process synchronization/lock when allocating or freeing memory on the heap, for example a semaphore or a mutex.
在 FreeRTOS 操作系统上,任务(线程)共享同一个堆,但每个任务都有自己的堆栈。这在处理低功耗低 RAM 架构时非常方便,因为同一个内存池可以被多个线程访问/共享,但这有一个小问题,开发人员需要记住同步 malloc 的机制并且需要 free,这就是为什么在堆上分配或释放内存时必须使用某种类型的进程同步/锁,例如信号量或互斥锁。