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?
8 Answers
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.
-
5Typically threads share resources, such as memory, so any non-braindead thread implementation would share the heap. Commented Nov 3, 2009 at 5:36
-
11The main reason each thread has its own stack is so that the thread can actually do something (like call a functions) ...– EdmundCommented Nov 3, 2009 at 6:08
-
3Every thread has a separate stack, but it is not necessarily 'private'. Other threads are usually allowed to access it.– zchCommented Dec 9, 2014 at 17:23
-
you will get a stack overflow.
A stack overflow on Stack Overflow! Commented Nov 30, 2016 at 10:33 -
2@crisron It's possible to set up a separate heap for each thread, but if you do that rather than using the default shared heap, then it becomes difficult for e.g. thread A to allocate a buffer, fill it with data, pass it to thread B, and have thread B use the data and then free the buffer (because thread B doesn't have access to thread A's heap, thread B can't free the buffer; the best thread B could do is pass the buffer back to thread A again and have thread A free it). Commented Jan 7, 2017 at 17:03
By default, C has only a single heap.
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.
-
By default C, and C++, don't have multiple threads. The 2003 c++ specification at least makes no allowances for threads in its virtual machine design, so threads, in c++, are implementation defined. Commented Nov 3, 2009 at 9:32
-
Even if different threads have different areas to allocate from on the heap, they can still see data allocated by another thread, so the threads do still share the same heap. Commented Nov 3, 2009 at 14:25
-
2Update: as of C++11 threads are no longer implementation defined. Commented Jul 5, 2012 at 19:25
Depends on the OS. The standard c runtime on windows and unices uses a shared heap across threads. This means locking every 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.
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).
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 structures used 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?
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.
-
I thought fork was more like creating a new process that just copied the data to a new memory location. Commented Nov 3, 2009 at 6:18
-
2fork() can serve in many use-cases where threads may be used also. Due to copy-on-write, there is no significant cost difference on Unix systems. Typical use-case is where the worker is autonomous (like web server) from the rest of the service. Another possibility is to communicate through stdin/out with the main thread/program. fork() is strong on Unix, whereas other platforms like Windows prefer threading. The main reason probably is that using fork() is much simpler and safer and Unix has this simplicity philosophy. See for example apache webserver, with its slow transition to threads.– ypnosCommented Nov 3, 2009 at 13:41
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.
Windows-Specific: TLS-space can be allocated using TlsAlloc and freed using TlsFree (Overview here). Again, it's not a heap, just DWORDs.
Strangely, Windows support multiple Heaps per 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.
EDIT: Some memory allocators (specifically jemalloc on FreeBSD) use TLS to assign "arenas" to threads. This is done to optimize allocation for multiple cores by reducing synchronization overhead.
-
> "Strangely, Windows support multiple Heaps per process.", it's not weird at all, one could use different heaps for different types of allocations, just adds more flexibility. Of course you can always get down to VirtualAlloc, and build your own heap however you want.– user90843Commented Oct 15, 2012 at 3:39
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.