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

Source: Internet
Author: User

Operating System-Thread (5) pop-up thread & make single-Thread code multithreading will produce those problems, thread Thread

Main content of this article

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.

Handling of pop-up threads: When a new request arrives, a thread is created immediately to process the request (pop-up thread pops up ).

Advantages of pop-up threads:

When using a pop-up thread, you need to consider that 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.

Ii. multithreading of Single-thread code

Some existing code is based on a single thread. If you change it to support multithreading, what will happen? Let's take a simple one-by-one analysis.

2.1 multi-threaded shared Variables

Figure:

N is shared between t1 and t2:

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 repeat

Similar to the above sharing problem, a Library provides a function. After a thread enters the Library, it does not return the result. 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)

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 the stack of a process is abnormal (stackoverflow), the kernel automatically allocates a stack for the process. When a process has multiple threads, it is bound to have multiple stacks, when the kernel does not fully understand all stacks and some stacks may encounter stack exceptions, the kernel does not know and cannot automatically allocate stacks for it.

2.5 all

If multithreading is introduced into an existing single-threaded system without much analysis and design, it will produce many unpredictable errors. It is not as simple as introducing the multi-threaded mechanism, it is necessary 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.

 

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.