CLR 中的堆栈管理
我了解堆栈和堆的基本概念,但如果有任何1可以解决以下困惑,那就太好了:
- 整个应用程序进程是否有一个堆栈,或者为项目中启动的每个线程创建一个新堆栈?
是否有一个堆用于整个应用程序进程或为项目中启动的每个线程创建一个新堆栈?
如果为每个线程创建堆栈,那么进程如何管理线程的顺序流(以及堆栈)
I understand the basic concept of stack and heap but great if any1 can solve following confusions:
Is there a single stack for entire application process or for each thread starting in a project a new stack is created?
Is there a single Heap for entire application process or for each thread starting in a project a new stack is created?
If Stack are created for each thread, then how process manage sequential flow of threads (and hence stacks)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每个线程都有一个单独的堆栈。这不仅适用于 CLR,也不仅适用于 Windows,而且几乎适用于每个操作系统或平台。
每个应用程序域都有一个堆。单个进程可以同时运行多个应用程序域。单个应用程序域可以运行多个线程。
更准确地说,每个域通常有两个堆:一个是常规堆,另一个用于非常大的对象(例如,64K 数组)。
我不明白你所说的“线程顺序流”是什么意思。
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.
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).
I don't understand what you mean by "sequential flow of threads".
每个线程一个堆栈,所有线程共享相同的堆。
不存在线程的“顺序流”。线程是存储处理器状态副本的操作系统对象。处理器状态包括寄存器值。其中之一是 ESP,堆栈指针。另一个非常重要的是 EIP,即指令指针。当操作系统在线程之间切换时,它将处理器状态存储在当前线程对象中,并从线程对象中为选择下一个运行的线程重新加载状态。处理器现在只是继续执行之前停止的地方。
启动一个线程现在也许也很容易理解。操作系统为堆栈分配一兆字节的内存。并初始化 ESP 寄存器值以指向该内存。并将EIP寄存器的值设置为线程应开始执行的方法的地址。 C# 中 ThreadStart 委托的值。
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.