The difference between threads and processes and the understanding of multithreading concurrency

Source: Internet
Author: User
Tags min mutex prepare relative sleep

Turn from:

Http://blog.sina.com.cn/s/blog_12f5264ff0102v8eq.html

http://blog.csdn.net/suxinpingtao51/article/details/8113509

http://blog.csdn.net/cqkxboy168/article/details/9026205/


First, the difference between the thread and the process the concept of threading and process is simply said:
(1) A process is an application that runs in memory, such as a Windows system in which a running EXE is a process. (2) A thread is an execution process in a process. Difference: A program has at least one process, and a process has at least one thread. An application can start multiple processes at the same time. For example, for IE browser program, each open an IE browser window, start a new process. While a thread is an execution process in a process, a process can have multiple threads, each of which performs different tasks, which is called concurrency when multiple threads in the process run concurrently. In addition, there is a very important difference between threads and processes: Each process has independent memory units during execution, while multiple threads in the same process share memory, which greatly improves the efficiency of the program.
Second, multi-threaded concurrency Understanding multithreading concurrency is only the surface and sensory concurrency, not the actual concurrency. A thread has to run, it must occupy the CPU, and most of the computers we use today are single CPUs, so at most one thread at a time can get the CPU and run. The essence of multithreading is to "make the most of CPU resources", so that when one thread does not need to consume CPU and only needs to deal with resources such as I/O, other threads have the opportunity to gain CPU resources. This is somewhat similar to the "overall approach", for example, let you clean the house and boil water, in the shortest possible time to do these two things, you will think of the first to boil, and then in the free time for the water to clean the house, instead of cleaning before you go to boil water, it is not first burned before the cleaning, this example, You are the only CPU, and boiling water and cleaning is two threads. Although the CPU has only one, but it switches between multiple threads frequently, when the frequency of switching is high to a certain extent, we feel that all the threads are running concurrently, and then feel that the multiple threads are concurrent. Therefore, concurrency does not really mean that multiple threads run concurrently, it simply describes a phenomenon. It is like saying that some people are "Iron Man", just used to describe someone who is not afraid of suffering, like a "Iron Man".


1. Concurrency: In the operating system, there are several programs in a period of time that are running from start to finish, and that these programs are running on the same processing machine. Two of these concurrency relationships are synchronous and mutex, respectively
2. Mutual exclusion: The phenomenon of the use of critical resources between processes is called mutex.
3. Synchronization: The relationship between processes is not the relationship of mutually exclusive critical resources, but the interdependent relationship. Further note: The output of the previous process is the input of the latter process, and the second process must wait when the first process does not output. A group of concurrent processes that have synchronization relationships send each other information called messages or events.
There is concurrent pseudo concurrency and true concurrency, pseudo-concurrency refers to the single-core processor concurrency, true concurrency refers to the multi-core processor concurrency.
4. Parallel: In a single processor in a multi-channel program design system, the process is alternately executed, showing a concurrency of external special; In multiprocessor systems, processes can be executed alternately and overlapped. A program on a multiprocessor can implement parallel processing. In this sense, parallelism is for multi-processor. Parallel is a concurrent occurrence of multiple simultaneous events, with the meaning of concurrency, but concurrency is not necessarily parallel, it is also said that concurrent events do not necessarily occur at the same time.

5. Multithreading: Multithreading is the logical layer concept of programming, which is a piece of code that runs concurrently in a process. Multithreading enables switching between threads to execute.

6. Async: Asynchronous and synchronous are relative, synchronization is sequential execution, execution of one after execution of the next, need to wait, coordinated operation. Asynchronous is independent of each other, in the process of waiting for an event to continue to do their own things, do not have to wait for the event to complete and then work. A thread is a way to implement Asynchrony. Async is the main thread that lets the calling method do not need to wait for another thread to finish synchronously, so that the main thread can Cheng Gan other things.
Async and multithreading are not an equal relationship, asynchronous is the ultimate goal, multithreading is just a means for us to implement Asynchrony. Asynchronous is when a call request is sent to the callee, and the caller can do something else without waiting for the return of the result. The implementation of Asynchrony can be handled by multithreading or by handing it to another process.


For a better understanding of the above concepts, let's say that I'm going to do boiling water, lifting the barbell 100, washing clothes 3 things.

Boil boiling water this thing, I want to do for, prepare to boil boiling water for 1 minutes, and so boiling water boil 8 minutes, turn off the boiling machine 1 minutes
Lifting the Barbell 100 under what I'm going to do for, lift the barbell 100 under 10 minutes
Wash the clothes I want to do for, prepare to wash clothes 1 minutes, wait for boiling water boil 5 minutes, turn off the washing machine 1 minutes

Single-Core case
To complete the synchronization, I need to do the time for 1+ 8 +1 + 10 + 1+ 5 +1 = 27 minutes

If it's asynchronous, it's waiting, I can switch to something else.

Prepare boiled water (1) + Ready to wash (1) + Lift 50 Barbell (5) min + Off washing machine 1 min + Lifting Barbell 20 under (2) min + Off boiling machine 1 min + Lifting 30 down Barbell (3) min
1+1+5+1+2+1+3 = 14 minutes


Dual-core asynchronous parallel

Nuclear 1 prepare to boil boiling water for 1 minutes + Lift Barbell 50 under (5) minutes + wait 3 minutes + turn off the boiler for 1 minutes

Nuclear 2 ready to wash clothes 1 minutes + lift Barbell 50 under (5) minutes + turn off the washing machine 1 minutes + wait 3 minutes

In fact, it only took 1+5+3+1 = 10 minutes

There are two cores waiting for 3 minutes.

Dual-core asynchronous non-parallel

Nuclear 1 Lifting Barbell 100 (10) min

Nuclear 2 prepare to boil boiling water 1 minutes + prepare to wash clothes 1 minutes + wait 5 minutes + + Turn off the boiler 1 minutes + wait 1 minutes + turn off the washing machine for 1 minutes

In fact, it only took 1+5+3+1 = 10 minutes

Multi-threaded approach
Single Core

Thread 1 prepare to boil boiling water for 1 minutes, wait for boiling water for 8 minutes, turn off the kettle for 1 minutes
Thread 2 Lifting Barbell 100 under 10 minutes
Thread 3 Prepare to wash for 1 minutes, wait for boiling water to boil for 5 minutes, turn off the washing machine for 1 minutes

The CPU may switch the most ideal switching mode

Thread 1 ready to boil boiling water 1 sleep 1 sleep 5 sleep 1 sleep 2 off boiling water 1 minutes exit
Thread 2 Sleep 1 sleep 1 Lifting Barbell 50 5 minutes sleep 1 Lifting Barbell 20 2 min sleep1 Lifting Barbell 30 next 3 minutes
Thread 3 Sleep 1 ready to wash clothes 1 minutes sleep 5 off washing machine 1 minutes exit

The last use of 14 minutes is the same as async.
But actually it is not the same, because the thread will not run according to our assumptions, if the thread 2 barbell first run, the speed of the entire process down.

The difference between asynchronous and synchronous, when IO waits, synchronization will not cut away, wasting time.

If all are exclusive CPU business, such as lifting the barbell business, in the case of single-core multi-line and single-wire no difference.

Multithreading benefits, it is easier to implement the idea of asynchronous switching, because the asynchronous program is difficult to write. Multithreading itself is still done synchronously, but it should be said
than efficiency is not as asynchronous. and the multi-line is very easy to write, relative efficiency is also high.

The benefit of multicore is that you can do things at the same time, which is completely different from the single core.

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.