Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threading heap and stack

How memory is allocated in case of spawning a new thread, i.e 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.

like image 520
DJ. Avatar asked Mar 15 '10 16:03

DJ.


3 Answers

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.

like image 124
JaredPar Avatar answered Oct 20 '22 21:10

JaredPar


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

like image 36
Seb Avatar answered Oct 20 '22 19:10

Seb


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.

like image 36
Andrey Avatar answered Oct 20 '22 20:10

Andrey