Synchronous asynchronous and blocking

Source: Internet
Author: User

The difference between synchronous/asynchronous and blocking/non-blocking I like to explain some concepts in my own language by contacting some of the phenomena in real life, and when I can do that, I understand the concept. The concept to be explained today is: synchronous/asynchronous versus blocking/non-blocking differences.

These two sets of concepts are often confusing, as they all involve IO processing, while others are similar.

The first is to explain the concept of synchronous and asynchronous, both concepts andnotification mechanism for messagesAbout.

For example, if I go to the bank for business, may choose to wait in line, may also take a small note with my number, wait until the line to my this number by the counter people inform me my turn to handle the business.
The former (queued) is the synchronous wait message, while the latter (waiting for someone else's notification) is waiting for the message asynchronously. In asynchronous message processing, waiting for a message (in this case, the person waiting for a business) often registers a callback mechanism that is triggered by the trigger when the event is triggered (the person at the counter here) Find the person waiting for the event by some mechanism (here is the number written on the little note).
In the actual program, synchronous message processing is like a simple read/write operation, they need to wait for both operations to succeed to return, and asynchronous processing mechanism is similar to the select/poll, such as multiplexing IO operations, when the attention of the message is triggered, The processing of messages triggered by a message-triggering mechanism.

Second, to explain the blocking and non-blocking, the two concepts andstate when a program waits for a message (no synchronization or asynchronous)About.
To continue the above example, whether it is queued or using a number to wait for notification, if in this waiting process, waiting for the wait, in addition to waiting for the message can not do other things, then the mechanism is blocked, in the program, that is, the program has been blocked at the function call can not continue to execute. Instead, Some people like to make a phone call and send text messages while the bank is in the process of waiting, the state is non-blocking, because he (the waiting) did not block on the message notice, but to do their own things while waiting. But note that the first synchronous non-blocking form is actually inefficient, Imagine you're on the phone, and you need to look up and see if the line is up to you. No, if the call and observation queue position as a program of two operations, the program needs to switch between the two different behaviors, the efficiency is imagined to be low, and the latter, asynchronous non-blocking form has no such problem , because the phone is your (waiting) thing, and notifies you is the counter (message trigger mechanism) thing, the program does not go back and forth between the two different operations.

Many people will confuse synchronization and blocking, I think because a lot of times synchronous operation will be in the form of blocking, for example, many people will write blocking read/write operation, but do not forget to set the O_NONBLOCK flag bit for FD, so that the synchronization operation can become non-blocking; Many people also confuse asynchronous and non-blocking, because asynchronous operations are generally not blocked at real IO operations, such as when a select function is returned to read, and then read will not be blocked, just as when your number is usually no longer there before you, So you go to the counter and do business without being blocked.

As can be seen, synchronous/asynchronous and blocking/nonblocking are two different sets of concepts, which may coexist together, or see here:
http://www.ibm.com/developerworks/cn/linux/l-async/

-----------------------------------------Split Line------------------------------------------------------
Last night after writing this article, this morning to see the feedback, at the same time read their own several times, found that there are some places to explain the lack of clarity, here continue to complement my statement, I hope no more said the more confused.

Synchronous and asynchronous: As mentioned above,synchronization and Asynchrony are just mechanisms for how the message is notified, not the mechanism for handling the messageThat isin the case of synchronization, it is up to the person processing the message to wait for the message to be triggered, and in the case of the asynchronous, the trigger mechanism notifies the person handling the message ., so in the asynchronous mechanism, the processing of the message between the trigger mechanism requires a link between the bridge, in our example, the bridge is a small note above the number, and in the Select/poll and other IO multiplexing mechanism is FD, when the message is triggered, The triggering mechanism finds the processing function for processing the FD through FD.

Please note that understandingmessage notification and processing messagesThese two concepts, this is the key to understanding the problem. or return to the above example, it is your turn to handle the business this is your concern, and to deal with the business is the processing of this information, the two are different. And in real IO operations, the message is that the FD is read/write, The processing of the message is to read and write to this FD. Synchronous/asynchronous just focus on how to notify the message, they do not care about how to deal with the message, as if the bank of the people just inform you that it is your turn to handle the business, and how to do business they do not know.

And a lot of people confuse synchronization and blocking, I think also because there is no distinction between the two concepts, such as blocking the read/write operation, in fact, the message notification and processing messages together, where the message is that the FD is read/write, and the processing of the message is the FD read/ Write. When we set this FD to non-blocking, the read/write operation does not block at the wait for message notification, and if the FD is unreadable/written, the operation returns immediately.

Many people will ask, asynchronous operation will not be blocked, right? has been informed that the message can be processed, it must not be blocked it?
In factasynchronous operations can be blocked, but they are not usually blocked while processing the message, but are blocked while waiting for the message to be triggered. For example, the Select function, if the last timeout parameter passed in is null, then if none of the events of interest is triggered, the program will always block at this select call. And if you use asynchronous non-blocking scenarios, such as aio_* group operations, when I initiate an AIO _read operation, the function will immediately return will not be blocked, when the event of interest is triggered by invoking the previously registered callback function for processing, see the link above me for the article. Back to the example above, if the person waiting for the business in the bank uses an asynchronous way to wait for the message to be triggered, That is, if he could not leave the bank to do other things during this time, it was clear that the man was stuck on the waiting operation; but the man suddenly found himself smoking and needed a cigarette, so he told the lobby manager that Call me when you get to my number. (Register a callback function), then he is not blocked on this waiting operation, naturally this is asynchronous + non-blocking way.

Synchronous asynchronous and blocking

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.