The difference between synchronous/asynchronous mode and blocking/non-blocking mode)

Source: Internet
Author: User

From http://www.cppblog.com/converse/archive/2009/05/13/82879.html

First, we will explain the concepts of synchronization and Asynchronization. Notification Mechanism of messages Related.

For example, if I go to a bank to handle business operations, I may choose to wait in queue or take a small note with my number, the person at the counter informed me that it was my turn to handle the business when I arrived.
The former (waiting in queue) means waiting for messages synchronously, while the latter (waiting for notifications from others) means waiting for messages asynchronously. in asynchronous message processing, the person waiting for the message (in this example, the person waiting for processing the Business)
A callback mechanism is triggered when a waiting event is triggered by a trigger mechanism (the person at the counter here) through a mechanism (here is the number written on a small note) find the person waiting for the event.
In the actualProgramSynchronous Message Processing is like a simple read/write operation. They need to wait until the two operations are successful before they can be returned. asynchronous processing mechanism is similar to select/poll and other multiplexing Io operations, when a message of interest is triggered, it is triggered by a message trigger mechanism notification to process the message.

Next, let's explain the two concepts of blocking and non-blocking:Status when the program waits for messages (synchronous or asynchronous)Related.
In the above example, whether waiting in queue or waiting for notifications by number, if the waiting person cannot do anything other than waiting for messages during the waiting process, the mechanism is blocked, shown in
In the program, that is, the program has been blocked and cannot continue to be executed in the function call. on the contrary, some people like to call and send text messages while waiting for these services in the bank.
Non-blocking, because he (the waiting person) is not blocked in the Message notification, but waits while doing his own thing. however, you must note that the first non-blocking synchronous mode is actually inefficient.
When you are on the phone, you still need to look up to see if the team has reached you. If you think of the call and the position of the observation queue as two operations of the program, this program needs to come between these two different actions
The efficiency of the switch back can be imagined to be low; while the latter, the asynchronous non-blocking mode does not have such a problem, because the call is your (waiting) thing, while notifying you is about the counter (message trigger mechanism,
The program does not switch back and forth in two different operations.

Many people will confuse synchronization and blocking. I think it is because many times the synchronization operation will be displayed as blocking. For example, many people will write blocked read/write operations, but don't forget to set
Set the o_nonblock flag to make the synchronization operation non-blocking. Similarly, many people will confuse asynchronous and non-blocking operations, because asynchronous operations are generally not in real Io operations
Blocking. For example, if the select function is used, it will not be blocked when the select function returns readable data and then reads data. It is like when your number is displayed, it is generally because you have no one, institute
If you go to the counter to handle the business, it will not be blocked.

It can be seen that synchronous/asynchronous and blocking/non-blocking are two different concepts, they can coexist and combine, or you can refer to here:

Http://www.ibm.com/developerworks/cn/linux/l-async/Synchronous and asynchronous: As mentioned above,Synchronization and Asynchronization are only about how messages are notified, rather than message processing.. That is to say,In synchronous mode, the message handler waits for the message to be triggered. In asynchronous mode, the message handler is notified by the trigger mechanism.Therefore, in the asynchronous mechanism, a connection bridge is required between the message handler and the trigger mechanism. In our example, the bridge is the number on the small note, in select/poll and other IO multiplexing mechanisms, FD is used. When a message is triggered, the trigger mechanism finds the processing function for processing the FD through FD.

Please understandMessage notification and processing messageThis is to understand the two concepts.
The key to the question. back to the example above, it is your turn to handle the business. This is the message you are concerned about, and to handle the business is to process the message, there is a difference between the two. in actual Io operations
The message concerned is whether the FD can be read and written, and the processing of the message is to read and write the FD. synchronous/asynchronous only focuses on how to notify messages, and they do not care about how to process messages, for example, silver
The person in the bank only informs you that it is your turn to handle the business, but they do not know how to handle the business.

Many people confuse synchronization and blocking because they do not distinguish these two concepts, such as blocking read/write operations, in fact, it is to combine Message notification and message processing
The message concerned here is whether the FD can be read/written, while the message is read/written to the FD. when we set this FD to non-blocking, the read/write operation will not be waiting for message communication.
If FD cannot be read or written, the Operation will return immediately.

many people may ask again, isn't asynchronous operations blocked? If a message has been notified that it can be processed, it must not be blocked, right?
In fact, asynchronous operations can be blocked, but they are not blocked when processing messages, it is blocked when the message is triggered .
for example, in the select function, if the last timeout parameter passed in is null, if no event is triggered, the program will always block the Select call
. if asynchronous non-blocking is used, such as AIO _ * Group operations, when I initiate an aio_read operation, the function will immediately return and will not be blocked, when the event of interest is triggered,
the callback function previously registered is called for processing, for more information, see Article . back in the above example, if the bank waits for the person to handle the business to wait for the message to be triggered asynchronously, that is, a small piece of paper is received, if he cannot leave the bank to do other things during this time, it is clear that this person is blocked in this waiting operation;, the man suddenly found himself
he had been addicted to smoking and needed to go out for a cigarette. So he told the lobby manager that, when I enter this number, please contact me outside (register a callback function), so he will not be blocked in this waiting operation
, naturally, this is the asynchronous + non-blocking method.

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.