Advantages and disadvantages of single thread and multithreading

Source: Internet
Author: User
Tags cpu usage

Advantages of multithreaded Processing

It is easier to develop a synchronous application, but it is usually less efficient than a multithreaded application because it requires that the previous task be completed before new tasks can be started. If the synchronization task takes longer to complete than expected, the application may not respond. Multithreading can run multiple processes at the same time. For example, a word processor application can check spelling (as a separate task) while you work on a document. Because multithreaded applications divide programs into separate tasks, you can significantly improve performance in the following ways:
Multithreaded technology makes the program more responsive because the user interface can be active while doing other work.
Tasks that are not currently being processed can give processor time to other tasks.
A task that consumes a lot of processing time can periodically give processor time to other tasks.
You can stop a task at any time.
You can prioritize each task individually to optimize performance.

Whether you need to create multithreaded applications depends on several factors. Multithreading is best used in the following situations:
A time-consuming or heavy-duty processor-intensive task blocks user interface operations.
Each task must wait for an external resource (such as a remote file or Internet connection).

For example, an INTERNET application "ROBOT" that tracks links on a WEB page and downloads files that meet certain criteria. This application can download individual files synchronously, or you can download multiple files simultaneously using multithreading. Multithreaded methods are much more efficient than synchronous methods because they can be downloaded even if the remote WEB server responds very slowly in some threads.

The following are examples of multithreading
Still in the age of DOS, people are looking for a multi-tasking realization. So there is a TSR type of background resident program, compared with the representative of the side KICK, Vsafe and other excellent TSR program, the appearance and application of this kind of program does give users a great convenience, such as side KICK, we can program in the editor without the state, It is convenient to edit the source program while compiling and running. However, the fatal flaw of a DOS single-task operating system is doomed to be impossible to develop a real multitasking program under DOS. Entering the WINDOWS3.1 era, this situation still has no fundamental change, one application can only do a thing. such as database queries, unless the application is well-compiled, the entire system will not respond to user input during the query.
Into the Windows NT and Windows 9X era, the situation has been completely improved, the operating system from the real sense of the implementation of multi-tasking (strictly speaking, Win9x is not). An application can have many threads of execution when needed, and each thread is a small execution program, and the operating system automatically shares CPU resources with each thread, ensuring that none of the threads will cause the system to deadlock. This way, when programming, you can move the time-consuming task into the background and accept the user's input in the foreground with another thread. For those programming tasks with high real-time requirements, such as network customer service, serial communication and other applications, the implementation of multithreading undoubtedly greatly enhances the usability and robustness of the program.

=====================================================================================

Cons: Increases the overhead of scheduling and management, brings some uncertainties, requires complex synchronization mechanisms, avoids deadlocks, and so on.
Benefits: Improve responsiveness to a certain extent, and make full use of CPU resources in multicore situations.

=====================================================================================

In the case of a single-threaded program execution, the running path of the program (which is processed) is sequential and must be handled well before it is executed.
Multithreading well, for example, the program can execute more than 2 similar operations at the same time, such as some search agents or bulk email multithreaded software, because the operation of the network will need to return information for a long time, but the CPU is idle, if it is a sequential execution,   It will take a long time to search for thousands of IPs. If you use multithreading, you can add other searches during the wait and wait, which can improve efficiency. However, the problem of multi-threaded and multi-process common resources should be considered the same, for some public resources or public variable access and modification to pay attention to special, need some lock what, there is a sequential problem consideration.
The purpose of multithreaded programming is to "make the most of CPU resources", when the processing of a thread does not need to consume CPU and only deal with resources such as I/o,oembios, so that other threads that need to consume CPU resources have the opportunity to gain CPU resources. Each program executes with a process, and each process must have at least one main thread. This thread is actually a clue to the execution of the process, and you can add additional threads to the process in addition to the main thread, which in some way can be seen as adding multitasking to an application. When the program is running, you can suspend or run these threads based on various conditions, especially in multi-CPU environments, where these threads are running concurrently. Multithreading is having multiple threads within a process. This allows an application to have multi-tasking capabilities. Multi-process technology can also achieve this, but the creation process of high consumption (each process has a separate data and code space), communication between processes inconvenient (message mechanism), process switching time is too long, which led to the proposed multithreading, for a single CPU (not open Hyper-threading), Only one thread can be executed at the same time, so if you want to implement multitasking, you can only get one time slice per process or thread, and in one time slice, only one thread executes, and then the other thread executes according to one of the policies. Since the time slice is very short, this gives the user the feeling that there are a lot of threads executing at the same time. However, there is a cost to thread switching, so if you take a multi-process, you need to switch the memory required for the process that the thread is affiliated to, which costs a lot of time. Thread switching costs little, and threads can share memory. So using multithreading is much less expensive to switch on than many processes. However, thread switching still takes time, so it takes more time for a process with two threads to execute than two times for a thread's process. The use of multithreading does not improve the execution speed of the program, but will reduce the speed, but for the user, can reduce the user's response time. The above results are only for single CPU, if the multi-CPU or CPU using Hyper-Threading technology, the adoption of multithreading technology will improve the execution speed of the program. Because a single thread is only mapped to one CPU, and multithreading is mapped to multiple CPUs, Hyper-Threading technology is inherently multithreaded, so it also speeds up the execution of the program.

====================================================================================

If a thread is deadlocked, the only thing that proves to be an application problem is not the thread's disadvantage.

The advantages of line threads for processes:
1. Low Overhead
2. Sharing of resources is good.

Line threads disadvantages for the process:
1, shared resources need to spend a certain amount of lock resources, synchronization is relatively complex.
2. A thread crash can cause the entire process to crash, which of course is problematic for its own application

====================================================================================

CPU is the time slice of the process to allocate the cup processing time, when a process in a synchronous way to complete a few things, the process must complete the first thing and then do the second thing, so in order to request the CPU to do the things to do. In this single-threaded working mode, if the cup is considered to be a total of 100 time slices, the CPU may have spent only 10 of the time slices to deal with the current process to do, just use the CPU 10% of the time slice, and other time is wasted, of course, In fact, the CPU's working mode is to do one thing after another, but the processing speed of the cup is very fast, processing the requested case quickly.

In order to improve CPU utilization, multi-threaded way to complete a few things at the same time do not interfere with each other, such as the current process to complete three things 1, 2, 3, then the CPU will be 10% of the time to deal with these 3 things, so that the CPU usage reached 30%, greatly improving the CPU utilization. The advantages of multithreading are particularly noticeable in dealing with special occasions. For example, download files, you want to download one side of the display progress while saving, in this case, if there is no multi-threaded, no unexpected words will generally block the main thread, such as the progress of the progress bar has not been downloaded with the amount of changes, to the entire form can not move, with multi-threaded to solve the problem well.

Here is a life example may be better to understand multithreading: go back to see your girlfriend cooking, normal, she will wash the dishes (meat) first put in the pot to boil, and then while washing other dishes or other things, such as: washing dishes, cleaning table ready to eat, people or a person, but she also do a few things, This can greatly improve efficiency. The total sentence is: The CPU will still spend the same amount of time to complete all the things, but multi-threading can let the CPU plug in the same time to do a thing, visually let the user feel that the computer at the same time to help him deal with a number of things, better to improve the user experience.

Once you understand the benefits of multithreading, be aware of the scenarios in which you should use multithreaded technology. Because not all cases with multithreading are good, because the case of multi-threaded, the CPU will take time to maintain, the CPU processing the request of each thread between threads will take time, so the general situation is not multi-threading, with sometimes instead of the outweigh the gains. Most of the time, to use the multi-threaded main is the need to deal with a large number of IO operations or processing needs to spend a lot of times and so on, such as: Read and write files, video image collection, processing, display, save and so on.

Original connection: http://blog.163.com/shi_shun/blog/static/23707849201010410516935/

Pros and cons of single-threaded and multithreaded (RPM)

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.