[C/C ++ multi-thread programming 5] deep understanding of pthread threads

Source: Internet
Author: User
Tags posix

Deep understanding of the pthread thread in multi-thread programming
 PthreadPOSIX threads is short for POSIXThread Standard.The first few blogs have provided you with a preliminary multi-thread concept. Before learning more about thread synchronization and other multi-thread core knowledge, you need to have a deep understanding of multithreading. Many people ignore or circumvent this part of content. The direct problem is that the learner cannot grasp the internal principle of multi-threaded programming, and the level of understanding is too low.
1. Process resources:Process resources include storage resources and other resources. Other resources include environment variables, addresses, files, etc. Storage resources, process memory allocation, blog [C parsing 3] C language memory allocation has good reference value. Multithreading processes are different:Static zone: stores global and static variables.Heap zone: dynamic distribution zoneThe above static zone, heap zone, and other resources are collectively referred to as shared resources of processes. Shared resources are shared by all threads of the process.Thread heap: heap maintained by the threadThread Stack: the stack maintained by the threadIn the preceding thread heap, the thread stack is a unique resource of each thread. the threads are independent of each other and are not shared. When a thread is created, the system allocates a stack to the thread.You may have discovered that the thread also maintains a stack while sharing the process heap.2. Thread private data:
In multi-thread programming, global variables of a process are stored in the shared data zone to be shared by all threads. At the same time, the thread can also have its own global variables, called the private data of the thread. Readers may ask, where is the thread's non-global variable? Don't forget that the non-global variable space is in the stack.
3. Thread extinction:
When a thread dies, the free thread stack will be released, returned to the system, and the private data of the thread will be released. Resources shared by threads, static zones, shared process stacks, and other resources will not be released with the destruction of threads because these resources are shared among threads. Note the differences between the thread heap and the shared process heap. The thread understands this point, and further learning will give you a deeper understanding.

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.