Operating System-Thread (5) pop-up Thread & amp; problems arising from the multithreading of Single-Thread code

Source: Internet
Author: User

Operating System-Thread (5) pop-up threads & Problems Arising from multithreading of Single-Thread code
The main content of this article is the Pop-up threads (Pop-up threads), which will occur when the Code of a single thread is multithreading. I. Pop-up threads) the following uses the processing of a Service after an http arrives as an example to describe the pop-up thread. In the above example, the traditional practice may be that a thread in the Service has been waiting for the arrival of the request. After the request arrives, the thread will begin to check whether the request is finally being processed. When the thread is processing the request, the subsequent request will be blocked until the thread finishes processing the current request. As shown in. Processing Method of the pop-up thread: when a new request arrives, a thread is created immediately to process the request (pop-up thread is displayed ). advantages of the pop-up thread: The thread is brand new and has no history. The request is not blocked when a request is created quickly, and the latency between the request and the START process is very small. When using the pop-up thread, you need to consider it extra, this thread should run well there. User space or kernel space. It is relatively easy to put the thread in the kernel, but because in the kernel, if the thread has problems, it will be more harmful than the thread in the user space. 2. multithreading of Single-threaded code some existing code is based on single-threaded code. If you change it to support multi-threaded code, the consequences will occur. The following is a simple one-to-one analysis. 2.1 multi-thread shared variable diagram: n is shared between t1 and t2: T1 calls the Check program to Check its own status, the n = 1 State obtained in T1. Before using n, the cpu is scheduled to T2. T2 calls the Check program to Check its status. The obtained n = 2 State CPU is scheduled to T2 again. At this time, n is equal to 2 and an error is returned. The problem above can be optimized. Let T1 and T2 cancel the sharing of N, and let them maintain their own status code N1, and N2 to avoid the above problem. 2.2 repeated entry is similar to the above sharing problem. A Library provides a function. After a thread enters the Library, it does not return. Another thread enters the Library again. What is the problem. For example, if the Library puts data into the buffer before the request is returned, another thread enters the Library to reset the data in the original buffer, this causes unpredictable consequences for the execution of the first thread. This problem allows the Library to provide a flag. When the Library is called, set the flag so that subsequent requests will be blocked to solve the problem, however, this reduces the parallel execution capability of the program. 2.3 signal (Interrupt Processing) such as a keyboard key down signal, should there be that thread for processing, do you want to pop-up a new thread with multiple threads, how does one thread modify the signal? Do you want to notify other threads of the same signal at the same time? The processing of different threads may be completely different, such as ctrl + c. Some threads are used for pasting and some are used to terminate the program. The processing of signals is already complicated in a single-threaded program, and multithreading doubles the complexity. 2.4 heap management in many systems, when a process's stack is abnormal (stackoverflow), the kernel automatically allocates a stack for the process. When a process has multiple threads, there are bound to be multiple stacks. When the kernel does not fully understand all stacks, it is possible that some stacks have stack exceptions, and the kernel does not know and cannot automatically allocate stacks to them. 2.5 If multithreading is introduced into an existing single-threaded system without much analysis and design, many unpredictable errors will occur, it is not as simple as introducing a multithreading mechanism. We need to analyze and design the Library and other aspects to ensure that multithreading is introduced when the thread is secure. The cost of introducing multithreading in the later stage is much higher than that of introducing multithreading In the first design.

Related Article

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.