none
多线程下的线程栈是如何分配的呢 RRS feed

  • Question

  • 大家好:

    我在学习.NET C# 的时候有一个问题没有搞清楚。 我们通常说,一个.Net应用程序启动的时候,会首先分配一个栈(Stack),这个栈存储了一些变量、静态变量....等等,然后通过对象的引用,把这些值都指向对应的托管堆中的一些类型中。

    我们在学习多线程的时候,也知道,计算机运行的最小单位,并不是进程,而是线程,所以,这个栈(Stack)应该是随着我们运行的线程来分配的。当一个线程起的时候,OS按照.Net开始为这个线程分配出栈(Stack)并在托管堆中初始化分配一些内存供Stack中的各个变量和对象引用。

    所以,问题来了,如果我有一个程序,同时起来了N个线程,那么是否意味着.Net要分配出多个堆栈?这些栈Stack又是怎么联系的,怎么存储的?
    比如,我有一个C#.NET应用程序服务,一旦起来了,它的主方法什么都没做,就负责其中N个线程,各个线程负责做各自需要的事情,那么,这个时候究竟有多少个堆栈?他们之间是否会存在一个主栈(Stack)用于装载其他的分栈呢?各个栈(Stack)之间的联系又是如何的?他们是怎么分布的呢?

    谢谢大侠们能给我排除这个疑问。

    • Moved by Arthur Vickers - MSFT Monday, January 24, 2011 8:33 PM General CLR question; not EF specific. (From:ADO.NET Entity Framework and LINQ to Entities (Pre-Release))
    Monday, January 24, 2011 3:40 PM

Answers

  •  

    When we create a new thread, a new stack is allocated by CLR, with 1MB memory space, the stack is used to store method local variable, or parameters passed to next method.

     

    A stack is the snapshot of a thread's execution, from the stack, we can know what a thread is doing, which method the thread is now executing; Generally, different threads have different Method Call Sequence, so their stacks store different data.

     

    If we use synchronization logic in our code (for example, use lock keyword), supposing thread A has entered the critical area, thread A's stack will keep on growing (because of calling other methods or allocating more local variables); when thread B trying to enter same area, a data structure in the Heap will be checked, the data structure can tell that the critical area is occupied by thread A, so thread B will wait there.

     

    Threads are scheduled by OS (according to the priority), so thread A can create/start thread B, but thread A cannot assign CPU time to thread B. from the perspective of OS, all threads are same with each other, no primary, no secondary.


    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by HappyBoy0529 Saturday, February 5, 2011 8:19 AM
    Thursday, January 27, 2011 3:29 AM

All replies

  • Hi HappyBoy,

    Thanks for posting in the MSDN forum. Howerver this is an English only forum. You may post your thread in the Chinese forum for better support.

    For your questions, there will be N stacks which are allocated by CLR when N threads are started. The stacks are sharing the same heep. This is the only relationship among them.

    SO there is no main stack or other stacks.

    If you have any other questions, please feel free to let me know.

     


    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 26, 2011 8:10 AM
  • Hi HappyBoy,

    Thanks for posting in the MSDN forum. Howerver this is an English only forum. You may post your thread in the Chinese forum for better support.

    For your questions, there will be N stacks which are allocated by CLR when N threads are started. The stacks are sharing the same heep. This is the only relationship among them.

    SO there is no main stack or other stacks.

    你好,我本来是在中文论坛发的,但是不知道怎么回事,过了一天以后,这个帖子就到了英文论坛了。

    I know that all applications developed by .net are use the same heap. Heap is allocated by the system, or by the framework.
    But what i want is how does the stack be allocated? If there are N stacks allocated by CLR when N threads start, what differences between these N stacks? If there are two threads should be synchronized, what's the differents between the two threads' stack?

     I am looking forward to your detailed answers!

    Wednesday, January 26, 2011 12:39 PM
  •  

    When we create a new thread, a new stack is allocated by CLR, with 1MB memory space, the stack is used to store method local variable, or parameters passed to next method.

     

    A stack is the snapshot of a thread's execution, from the stack, we can know what a thread is doing, which method the thread is now executing; Generally, different threads have different Method Call Sequence, so their stacks store different data.

     

    If we use synchronization logic in our code (for example, use lock keyword), supposing thread A has entered the critical area, thread A's stack will keep on growing (because of calling other methods or allocating more local variables); when thread B trying to enter same area, a data structure in the Heap will be checked, the data structure can tell that the critical area is occupied by thread A, so thread B will wait there.

     

    Threads are scheduled by OS (according to the priority), so thread A can create/start thread B, but thread A cannot assign CPU time to thread B. from the perspective of OS, all threads are same with each other, no primary, no secondary.


    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by HappyBoy0529 Saturday, February 5, 2011 8:19 AM
    Thursday, January 27, 2011 3:29 AM
  •  

    When we create a new thread, a new stack is allocated by CLR, with 1MB memory space, the stack is used to store method local variable, or parameters passed to next method.


    Hi eryang:

    Thank you for your replies. I also have a questions about this case:
    Now I know, If there are N stacks run in my application, there will be N stacks in the memery. But does it exists a main stack just like a main method in our application?

    We always get an issue is:
    When we create a new thread in our winform and the thread is a normal thread, sometimes we use the thread to do a lot of things, then the UI will be dead. So, i think there must be something connect the two threads with each other. How should i understand it?

    Thank you for your patiently answered!

     

    Thursday, January 27, 2011 1:38 PM
  • Hi

    Your issue is unclear. Can you please provide some code or steps to reproduce your issue?

     


    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 31, 2011 2:25 AM
  • If I understood correctly, you want to know why the user interface is dead while the thread processes something else, giving you the sense that a certain thread is different or is more important than others.

    If I understood correctly, I can tell you that there is in fact a UI thread, that could be regarded as more important than other threads because it executes the user interface, and a frozen user interfaces doesn't look good at all.  But the CPU and the operating system will not treat the UI thread any differently than worker threads, for example.

    If you still are unclear, continue posting your questions.


    MCP
    Tuesday, February 1, 2011 5:56 AM
  • Hi all,

    I think i should read more knowledges about CLR. I'll ask the unsolved problems by a new thread if i can't understand it. Before ask here, I'll study it first.

    Becase i am developing a huge system services with a lots of threads to run all the time. I must know how do the threads work how the CLR managed each stack of the threads. I must save computer resources so that all the servers may run more efficient. 

    Thanks for replies, especially to eryang!

    Happy Boy

    Saturday, February 5, 2011 8:28 AM