Synchronous. Asynchronous. Blocking

Source: Internet
Author: User

Recently encountered some common concepts, especially the concept of network programming, such as: blocking, non-blocking, asynchronous I/O and so on, for these concepts themselves are not too clear understanding, but very vague concept, said understand it also understand, but to let oneself accurately describe the specific details of the concept, but said not so accurate, This is the reason why they have not been carefully looked at in these aspects. After looking at some of these concepts of information, found that synchronous, asynchronous, blocking, non-blocking concept is not difficult to understand, write this article, welcome to shoot bricks, hope to communicate.


1 Synchronous vs. asynchronous


The first is to explain the concepts of synchronization and asynchrony, which are related to the notification mechanism of a message. That is, synchronous and asynchronous are mainly from the perspective of the message notification mechanism.


1.1 Concept Description


The so-called synchronization is the completion of a task needs to rely on another task, only wait for the task to be relied upon to complete, the dependent task can be counted, this is a reliable task sequence. Either success succeeds, failure fails, and the state of the two tasks remains the same.


The so-called asynchronous is not required to wait for the task to be relied upon to complete, but to inform the dependent task to complete what work, dependent on the task is also immediately executed, as long as the completion of their own complete task, even if completed. As to whether the dependent task is ultimately completed, the task that relies on it cannot be determined, so it is an unreliable task sequence.


1.2 Message Notifications


Asynchronous concepts and synchronization are relative. After a synchronous call is issued, the caller waits for the return message (result) to be notified before subsequent execution can be performed; the caller cannot immediately get a return message (the result) when an asynchronous procedure call is made. The part that actually handles the call notifies the caller via status, notification, and callback after completion.


It is mentioned here that the executing parts and callers return results in three ways: status, notifications, and callbacks. Which notification mechanism to use depends on the implementation of the execution part, which is not controlled by the caller unless the execution part provides multiple choices.


If the execution part is notified by the state, then the caller needs to check every time, the efficiency is very low (some beginners of multithreaded programming, always like to use a loop to check the value of a variable, which is actually a very serious error);


If you are using notifications, the efficiency is high because there is almost no extra action required to execute the part. As for the callback function, there is not much difference from the notification.


1.3 Scene metaphor


For example, if I go to a bank for business, there may be two ways:


Choose to wait in line;


Another option to take a small note on the top of my number, wait until the line to my number by the counter to inform me that my turn to handle the business;


The first kind: The former (Waiting in line) is the synchronous waiting for the message notification, that is, I have been waiting for the bank to handle business situation;


The second: the latter (waiting for others to be notified) is an asynchronous wait for a message notification. In asynchronous message processing, waiting for a message notifier (in this case the person waiting for business) tends to register a callback mechanism that is triggered by the triggering mechanism (the person who is the counter here) through some mechanism (here is the number that is written on the small note, shouting) to find the person waiting for the event.


2 blocking and non-blocking


The two concepts of blocking and non-blocking are related to the state of a program (thread) waiting for a message to be notified (regardless of synchronous or asynchronous). This means that blocking and non-blocking are primarily the state perspective of the program (thread) waiting for a message notification.


2.1 Concept Description


A blocking call means that the current thread is suspended until the call results are returned, waits for a message notification, and is not able to perform other business. Functions are returned only after the result is obtained.


One might equate blocking calls with synchronous invocations, in fact they are different.


For synchronous calls, many times the current thread may still be active, but logically the current function does not return, and the thread may also process other messages. Also, here's the first extension:


If this thread is still performing other message processing while waiting for the current function to return, this is called synchronous non-blocking;
If this thread waits for the current function to return without performing other message processing, but is in a pending state, then this is called synchronous blocking;
So there are two ways to implement synchronization: synchronous blocking, synchronous non-blocking, and, similarly, there are two implementations of Asynchrony: asynchronous blocking, asynchronous non-blocking;


For a blocking call, the current thread will be suspended waiting for the current function to return;


The concept of non-blocking and blocking corresponds to a function that does not block the current thread and returns immediately until the result is not immediately available. Although the apparent non-blocking way can significantly improve the CPU utilization, but also with the other consequence is the system's thread switching increased. Increased CPU execution time can compensate for the switching cost of the system needs to be evaluated well.


2.2 Scene metaphor


To continue the above example, whether queued or using a number to wait for notification, if in the process of waiting, waiting for the wait, in addition to waiting for the message to 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.


On the contrary, some people like to call and send text messages while the bank is doing these operations, which is non-blocking because he (the waiting person) does not block on the message, but waits while doing his own thing.


However, it is important to note that the synchronous non-blocking form is actually inefficient, and imagine that you need to look up while you are on the phone. If the telephone 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 asynchronous non-blocking form does not have such a problem, because the phone is your (waiting) thing, and inform you is the counter (message triggering mechanism) , the program does not switch back and forth between two different operations.


3 synchronous/Asynchronous vs. blocking/non-blocking


The efficiency of the synchronous blocking form is the lowest,


In the example above, you are in line and doing nothing else.


In the actual program: is not the FD set o_nonblock flag bit read/write operation;


Asynchronous blocking form if the person waiting for the business in the bank uses an asynchronous way to wait for the message to be triggered (notification), that is, to receive a small note, if he cannot leave the bank to do other things during this time, it is obvious that the person is blocked on the waiting operation;


An asynchronous operation can be blocked, except that it is not blocked while processing the message, but is blocked while waiting for a message notification.


such as 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 the select call.


The 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 telephone and observation queue position as a program of two operations, the program needs to switch between the two different behaviors, efficiency can be imagined is low.


Many people write blocking read/write operations, but don't forget that you can set the O_NONBLOCK flag bit on FD so that the sync operation becomes nonblocking.


Asynchronous non-blocking forms are more efficient,


Because the phone is your (waiting) thing, and inform you is the counter (message trigger mechanism) thing, the program does not go back and forth between the two different operations.


For example, the person suddenly found himself addicted to smoking, need to go out to smoke a cigarette, so he told the lobby manager said, when the line to my number of trouble to notify me outside (register a callback function), then he is not blocked on this waiting operation, naturally this is asynchronous + non-blocking way.


If you use asynchronous non-blocking situations, such as the operations of the Aio_* group, when a aio_read operation is initiated, the function returns immediately and is not blocked, and the previously registered callback function is invoked when the event of interest is triggered.


Many people will confuse synchronization and blocking, I think because many times the synchronization 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. But most fundamentally because there is no distinction between the two concepts, such as blocking the read/write operation, in fact, the message notification mechanism and the status of waiting for the notification of the state together, where the interest is the message is whether FD is read/write, and wait for the status of the message notification is the FD readable/ Write the state of the program (thread) during the wait. When we set this FD to non-blocking, the read/write operation will not be blocked waiting for a message notification, if the FD is unreadable/write the operation returns immediately.


Similarly, many people 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 the read is not blocked, but is blocked at the Select function call.


4 Xiao Ming's story


Another scenario for the concept described above has been made clear that the synchronous/asynchronous focus is on the mechanism of message notification, while blocking/nonblocking is concerned with the state of the program (thread) waiting for a message notification. Take xiaoming to download the file for example, from these two concerns to explain the two sets of concepts again, hoping to better promote the understanding of everyone.


Sync blocking: Xiao Ming has been staring at the download progress bar, completed by the time of 100%.


Synchronization is reflected in: Waiting for the download to complete the notification;
Blocking is reflected in: Waiting for the download to complete the notification process, can not do other tasks processing;


Synchronous non-blocking: Xiaoming After submitting the download task to do something else, every time after a glance at the progress bar, see 100% to complete.


Synchronization is reflected in: Waiting for the download to complete the notification;
Non-blocking is reflected in: Waiting for the download to complete the notification process, to do other tasks, but occasionally will take a glance at the progress bar; "Xiaoming must switch between two tasks and follow the download Progress"


Asynchronous blocking: Xiao Ming changed a download to complete the notification function of the software, the download is done "ding" a sound. But Xiaoming still waits for a "ding" sound (looks silly, doesn't it).


Asynchronous reflected in: Download complete "ding" a notice;
Blocking is reflected in: Waiting for the download to complete the "ding" notification process, can not do other tasks to deal with;


Asynchronous non-blocking: still is that will "ding" a sound of the download software, xiaoming submitted the download task to go to do other, hear "ding" a sound will know to complete.


Asynchronous reflected in: Download complete "ding" a notice;
Non-blocking reflected in: Waiting for the download to complete the "ding" a notification process, to do other tasks, just to receive "ding" sound notification can be; "Software processing download task, Xiao Ming processing other tasks, do not need to pay attention to progress, just receive software" Ding "sound notification, you can"


That is, synchronous/asynchronous is the "Download complete message" notification (mechanism), while blocking/non-blocking is waiting for the "Download complete message" Notification process status (can do other tasks), in different scenarios, synchronous/asynchronous, blocking/non-blocking four combinations have been applied.


So, in summary, synchronous and asynchronous are just the mechanisms of concern about how messages are notified, while blocking and non-blocking are concerned with the state of waiting for a message notification. In other words, in the case of synchronization, it is up to the processing message to wait for the message to be triggered, while the asynchronous case is triggered by the trigger mechanism to notify the processing of the message, so in the asynchronous mechanism, the processing of the message and the trigger mechanism between the need for a bridge between the connection:


In the case of the bank, the bridge is the number on the small note.
In Xiao Ming's example, this bridge is the software "ding" sound.


Finally, it is important to understand the concepts of "message notification mechanism" and "state when waiting for message notification", which is the key to understanding the four concepts.

Synchronous. Asynchronous. 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.