views:

100

answers:

4

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.

A: 

Each thread has its own stack, but all threads share heap.

Andrew Bezzub
+1  A: 

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

Seb
+1  A: 

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.

Andrey
+2  A: 

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.

JaredPar
I think ms windows CLR implementation of threads is very close to native. Please specify example where non 1-1 mapping take place.
Andrey
@Andrey this is unfortunately not the case. Consider the addition of Thread.ManagedThreadId in 2.0. This property was added precisely for the purpose of distinguishing managed thread's from their potentialy many native backers.
JaredPar
@Andrey (cont) when the managed thread is an STA I do not believe the CLR can or will change the native thread since COM objects created would have thread affinity to the native thread. But in the MTA there is no affinity problem and the CLR can freely change the backing native thread.
JaredPar
I was trying to find the max number of thread a windows application can have without any noticeable performance degradation (cost). But was trying to clear the basics first, as in how the memory allocation is done when a new thread is spawned. I understand that spawning a thread is costly.
DJ
when you do new Thread() new thread is really spawned. you can see it is either debug or in task manager. if you want to find max number of threads just spawn them in loop and see what was maximum count
Andrey
@JaredPar i read all that stuff about ManagedThreadId but i never saw something about swapping threads or non 1-1 mapping in real life, although i write a lot of multithread code. also how would TLS and other that thread stuff work?
Andrey
@Jared: that was a project for the SQL Server group. It was abandoned, they couldn't get it reliable. Right now, all known CLR hosts have a 1-to-1 mapping between managed threads and OS threads. Link: http://blogs.msdn.com/dinoviehland/archive/2005/09/15/469642.aspx
Hans Passant
@Andrey, you need to differentiate native and managed TLS. This model would obviously have implications on native TLS code. The managed TLS model though ,`Thread.AllocateDataSlot`, is implemented independent of native TLS.
JaredPar
@nobugz it is still a part of the CLR hosting model to allow multiple native threads to back a given managed thread. Fiber mode was cut but this feature was maintained in the hosting model.
JaredPar
@JaredPar you might say right things, but this is very theoretical. please give example, or article with example where non 1-1 mapping can be touched.
Andrey