Overview of synchronous, asynchronous, multithreading and event

Source: Internet
Author: User

First, you need to understand what is blocking and blocking IO.
If a thread encounters disk read/write or network communication (collectively referred to as I/O operations) during execution, it usually takes a long time. At this time, the operating system will deprive the thread of CPU control, pause the execution and give the resource to other working threads. This thread scheduling method is called blocking. When I/O operations are completed, the operating system removes the thread's blocking status, restores its control over the CPU, and enables it to continue execution. This IO mode is synchronous IO or blocking IO.

The second is to understand what asynchronous IO is.
Correspondingly, asynchronous IO, that is, non-blocking IO, does not adopt blocking policies for all IO operations. When a thread encounters an IO operation, it does not wait for the completion of the IO operation or the return of data in a blocking way, but sends the IO request to the operating system and continues to execute the next statement. When the operating system completes the IO operation, the thread that executes the IO operation is notified in the form of an event. The thread will process the event at a specific time. To process asynchronous IO, threads must have an event loop, constantly check whether there are any unprocessed events, and process them in turn.

In blocking mode, a thread can only process one task. To increase the throughput, you must use multiple threads. In non-blocking mode, a thread is always performing computing operations. The CPU core utilization of this thread is always 100%, and IO is notified by event. In blocking mode, multithreading often improves the system throughput because other threads are working when one thread is blocked. multithreading can prevent the CPU resources from being wasted by the blocked threads. In non-blocking mode, threads are never blocked by I/O, And the CPU is always used. The benefit of Multithreading is only that more cores are used in the case of multi-core CPUs, and the single thread of Node. js can also bring the same benefits. This is why Node. js uses a single-thread, non-blocking event programming mode.

What is better for single-thread event-driven asynchronous I/O than traditional multi-thread blocking I/O? In short, asynchronous IO reduces the overhead of multithreading. For the operating system, it is expensive to create a thread. You need to allocate memory to it and include it in scheduling. At the same time, you need to execute memory paging during thread switching, and the CPU cache is cleared, when switching back, you need to read information from the memory again, undermining the data locality.
 
Of course, the disadvantage of asynchronous programming is that it does not conform to people's general programming thinking, and it is easy to obscure the control flow, which brings great difficulties to coding and debugging. Developers who are used to the traditional programming model tend to be at a loss when they are new to large-scale asynchronous applications. Despite this, asynchronous programming is still relatively difficult, but fortunately there are already many libraries dedicated to solving asynchronous programming problems (such as async ).

Comparison between synchronous IO and asynchronous IO
------------------------------------------------------------------
Synchronous IO asynchronous IO
------------------------------------------------------------------
Using multithreading to increase the throughput of a single thread can achieve high throughput
Multi-core CPU utilization using time slice splitting and Thread Scheduling
A single process can be bound to a single-core CPU by using multi-threaded OS scheduling.
It is difficult to make full use of CPU resources.
Large Memory tracks, weak Data Locality, small memory tracks, and strong Data Locality
Linear Programming thinking is not in line with traditional programming thinking

------------------------------------------------------------------

The multi-threaded model also has corresponding solutions, such as lightweight threads. It is highly controversial who is better in the event-driven single-thread asynchronous model and multi-thread synchronous model, because despite resource consumption, the latter's throughput is not lower than the former.


 

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.