开发者

stack management in CLR

I understand the basic concept of stack and heap but great if any1 can solve 开发者_开发问答following confusions:

  1. Is there a single stack for entire application process or for each thread starting in a project a new stack is created?

  2. Is there a single Heap for entire application process or for each thread starting in a project a new stack is created?

  3. If Stack are created for each thread, then how process manage sequential flow of threads (and hence stacks)


  1. There is a separate stack for every thread. This is true not only for CLR, and not only for Windows, but pretty much for every OS or platform out there.

  2. There is single heap for every Application Domain. A single process may run several app domains at once. A single app domain may run several threads.
    To be more precise, there are usually two heaps per domain: one regular and one for really large objects (like, say, a 64K array).

  3. I don't understand what you mean by "sequential flow of threads".


One stack for each thread, all threads share the same heaps.

There is no 'sequential flow' of threads. A thread is an operating system object that stores a copy of the processor state. The processor state includes the register values. One of them is ESP, the stack pointer. Another really important one is EIP, the instruction pointer. When the operating system switches between threads, it stores the processor state in the current thread object and reloads the state from the thread object for the thread that was selected to run next. The processor now simply continues executing where it left off previously.

Getting a thread started is perhaps now easy to understand as well. The operating system allocates a megabyte of memory for the stack. And initializes the ESP register value to point to that memory. And sets the value of the EIP register to the address of the method where the thread should start executing. The value of the ThreadStart delegate in C#.


Each thread must have it's own stack, that's where local variables and parameters are held, and the return addresses of the previous functions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜