线程化堆和栈
产生新线程时如何分配内存,即内存堆、内存堆栈和线程如何相关?我知道这是基本的(.net 框架概念),但不知何故我不太了解这个概念。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于 .Net 线程的实现方式,这个问题确实很难回答。托管线程和相应的本机线程之间不一定是 1-1 的实现。 CLR 可以自由地使用多个本机线程来实现单个托管线程。因此,分配新的托管线程并不一定会导致生成本机线程。它可以简单地假设一个现有的。
您能告诉我们为什么您对此感到担忧吗?也许这会让我们得到更好的答案。
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.
它的基础性比.net 深得多。线程是操作系统本机对象。所谓的托管线程只是本机线程的包装。
回到你的问题。内存堆在同一进程的线程之间共享,因为它们位于单个虚拟内存空间中。堆栈是单独的。
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.
每个线程都有自己的堆栈,但所有线程共享堆。
Each thread has its own stack, but all threads share heap.