The difference between heaps and stacks in multi-threading

Source: Internet
Author: User

Many modern operating systems, a process of (virtual) address space size of 4G, divided into the system space and user space two parts, the system space for all processes to share, and user space is independent, the general Windows process user space is 2G. all threads in a process share the process's address space , but they have their own (private) stack (stack), and the default stack size for Windows threads is 1M. Heap allocation differs from stack , typically a process has a C run-time heap, which is shared by all threads in the process, the Windows process also has the so-called process default heap, and users can create their own heaps. Heap: Is a common space for everyone, divided into global heap and local heap. The global heap is all unallocated space, and the local heap is the space allocated by the user. HeapWhen the operating system is initialized to the process of allocation, the operation can also be to the system to the additional heap, but remember to return to the operating system, or memory leaks. Stack: is a thread-unique, saving its running state and local automatic variables.The stack is initialized at the beginning of the thread, and the stack is independent of each other, so the stack is the thread safe.。The operating system switches the stack automatically when the thread is switchedis to toggle the SS/ESP register. Stack space does not require explicit allocation and deallocation in high-level languages.

Reference: Relationship between process threads and stack heap (go)

An article:

When it comes to multi-threaded routines, there are often some things that are difficult to Incredibles, and allocating a variable with heaps and stacks can produce unexpected results in subsequent executions, and the result is that the memory is being accessed illegally, causing the contents of the memory to be changed.

The two basic concepts for understanding this phenomenon are that the threads in a process share the heap, while threads in the process maintain their own stacks.

Another mechanism is to declare a member variable such as Char name[200], with the end of this code call, the address of name on the stack is freed, and if it is char * Name = new char[200]; The situation is completely different, unless a call to delete is displayed and the address pointed to by name is not freed.

Understanding the thread to the stack visibility, and memory management mechanism can be inferred from the beginning of the phenomenon of the author.

Use an example to understand this mechanism in depth.

In thread 1,

A ()

{

B ();

C ();

}

B ()

{

Stack or heap allocation variable V;

Insert the address of V into the public queue;

}

In thread 2:

D ()

{

while (1)

{

Processing of public queues;

}

}

In B, if you allocate the declaration V and the heap area using the mechanism of the temporary variable in the stack area, the result is different. If you use the stack area, if the variable address is am1-am2 so large, exit B calls when the address is released, the C function may overwrite the memory, so that when D executes, the content read from the memory am1-am2 is changed.

And if you allocate with new (heap), that is not the case, because there is no display of the pair with delete and the heap is shared with the thread, that is, 2 threads can see what the 1 threads are allocating in the heap, so there will be no false write.

Http://blog.sina.com.cn/s/blog_5c4aa6bc0100gdbm.html

MORE: http://blog.csdn.net/gykimo/article/details/9132157

The difference between heaps and stacks in multi-threading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.