Thread-shared resources and unique resources for the same process

Source: Internet
Author: User

Thread-shared environments include the process code snippet, the public data of the process (which leverages the shared data, the threads are easily communicating with each other), the file descriptor that the process opens, the processor of the signal, the current directory of the process, and the process user ID and process group ID.

The process has its own personality as well as its many commonalities. With these personalities, threads can achieve concurrency. These personalities include:
1. Thread ID
Each thread has its own thread ID, which is unique in this process.     The process uses this to identify the thread. 2. Value of the Register group
Because the threads are running concurrently, each thread has its own different running threads, and when switching from one thread to another, the state of the Register collection of the original thread must be saved so that the thread can be restored in the future when it is re-switched to. 3. The stack of threads
The stack is necessary to ensure that the thread runs independently.
A thread function can call a function, and the called function can be nested in layers, so the thread must have its own stack of functions so that the function call can execute normally, not affected by other threads.
4. Error return code
Since many threads in the same process are running concurrently, it is possible for a thread to set the errno value after a system call, while the thread has not handled the error and another thread is running at this point by the scheduler, so that the error value can be modified.
Therefore, different threads should have their own error return code variable.
5. Thread's Signal Shield code
Because each thread is interested in a different signal, the thread's signal masking code should be managed by the thread itself. But all the threads share the same signal processor.
6. Priority of Threads
Because the thread needs to be dispatched like a process, there must be a parameter available for dispatch, which is the thread's priority. When it comes to multi-threaded routines, there are often 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. On platforms such as Windows, different threads use the same heap by default, so when you allocate memory with malloc (or Windows GlobalAlloc) in C, you use synchronous protection. Without synchronous protection, a race condition occurs when two threads perform simultaneous memory operations, which can lead to confusion in memory management within the heap. For example, two threads are allocated a unified block memory address, an idle list pointer error, and so on.

Symbian threads generally use a separate heap space. This allows each thread to be allocated and freed directly in its own heap, reducing the overhead that synchronization introduces. When the thread exits, the system reclaims the thread's heap space directly, and the memory space within the thread does not cause a memory leak within the process.

However, when two threads use a common heap, they must be protected synchronously with the critical section or mutex. Otherwise, the program crashes sooner or later. If your thread needs to allocate and release any number and type of objects on the shared heap without rules, you can customize a allcator to use synchronous protection within allocator. The thread uses this allocator to allocate memory directly. This is equivalent to implementing your own malloc,free. However, it is recommended that you review your system again, because most of this is unnecessary. With good design, the local heap of threads should be able to meet the needs of most objects. If you have a class of objects that need to be created and shared on a shared heap, this is a reasonable requirement, and you can implement shared protection on new and delete on this class.

Thread-shared resources and unique resources for the same process

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.