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.