Operating System-thread (5) Pop-up threading && multithreading of single-threaded code can produce those problems

Source: Internet
Author: User

The main content of this article

    1. Pop-up threading (pop-up threads)
    2. Multithreading of single-threaded code can create problems
One, pop-up threading (pop-up threads)

The popup thread is introduced as an example of the processing of a service after an HTTP arrival.

The traditional practice in the above example is that a thread in the service has been waiting for the request to arrive, and when the call arrives, the thread will begin to check that the requests are finally processed. When this thread is processing the request, the request behind it will be blocked until the thread has finished processing the current request. As shown in.

pop-up threading : When a new request arrives, create a thread to process the request (pop pop-up thread).

Advantages of pop-up threading:

    1. Threads are brand new, without history, created quickly
    2. Request is not blocked by block, and the latency between requests to reach start processing is very small

There is a need for extra consideration when using pop-up threads, which should be better to run in. User space or kernel space. It is relatively easy to put a thread in the kernel, but because in the kernel, if the thread goes wrong, the harm will be greater than the thread of the user space.

second, multithreading the single thread code

Some of the existing code is based on single-threaded, if it is modified to support multithreading will have those consequences, the following simple analysis.

2.1 Multi-threaded shared variables

Icon:

N is shared between T1 and T2:

    1. T1 calls the check program to check its status, n=1 this state
    2. The CPU is dispatched to T2 before T1 gets the state using N.
    3. T2 calls the check program to check its status, n=2 this state
    4. The CPU is dispatched to T2 again, at which point N is already equal to 2 and produces the wrong result.

The above problem is can be optimized, let T1 and T2 cancel the share of N, let it each maintain their own status code N1, and N2 can avoid the above problems.

2.2 Re-entry

Similar to the sharing issue above, a library provides a feature that will cause problems when a thread enters the library and does not return before another thread enters.

For example, if the library puts the data into buffer before the request is returned, then another thread enters the library to reset the original buffer data, thus causing unpredictable consequences for the execution of the first thread.

This problem allows the library to provide a flag that, when the library is in the calling state, sets the flag, so that subsequent requests will be blocked to resolve the problem, but this will reduce the program's ability to execute concurrently.

2.3 Signal (interrupt handling)
    1. For example, a keyboard key down signal, should have that thread processing, do not pop-up a new thread
    2. How does a thread modify a signal when there are multiple threads, and do you want to notify other threads at the same time
    3. The same signal, different threads can be handled completely differently, such as CTRL + C, some threads for pasting, and some for terminating the program.

Signal processing in a single-threaded program is already very complex, multithreading is the complexity of double.

2.4 Heap Management

In many systems, when a process stack exception (StackOverflow), the kernel automatically allocates the stack for this process, when a process has more than one thread, there is bound to be multiple stacks, when the kernel does not fully understand all the stack, it is possible that some stacks occur stack exception, the kernel does not know, The stack cannot be automatically allocated for it.

2.5 All

If the introduction of multithreading into an existing single-threaded system without a lot of analysis and design will produce many unpredictable errors, it is by no means a simple introduction of multithreading mechanism, to analyze and design from various aspects of the library, to ensure that the thread security is introduced in the case of multithreading. The cost of introducing multithreading at a later stage is much higher than the first design that includes multithreading.

Operating System-thread (5) Pop-up threading && multithreading of single-threaded code can produce those problems

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.