Threading heap and stack

How memory is allocated in case of spawning a new thread, ie how memory heap, memory stack, and threads are related? I know this is fundamental (.net framework concept) but somehow I am not much aware of this concept.


It's really hard to answer this question because of the way .Net threads are implemented. There is not necessarily a 1-1 implementation between managed and corresponding native threads. The CLR is free to use multiple native threads to implement a single managed thread. So allocating a new managed thread does not necessarily cause a native thread to be spawned. It can simple assume an existing one.

Can you tell us why this is of concern for you? Perhaps that will lead us to a better answer.


The stack belongs to the thread context. The heap belongs to the process, it is hence shared between the threads.


It is fundamental much deeper than .net. Threads are OS native objects. What is called Managed Thread is just wrapper around native thread.

So back to your question. Memory heap is shared accross threads of same process because they are located in single virtual memory space. Stacks are individual.

链接地址: http://www.djcxy.com/p/14518.html

上一篇: JVM堆分配或栈分配框架?

下一篇: 线程堆和堆栈