Talk about synchronous, asynchronous, blocking, and non-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

首先来解释同步和异步的概念,这两个概念与消息的通知机制有关。也就是同步与异步主要是从消息通知机制角度来说的。

1.1 Concept Description

所谓同步就是一个任务的完成需要依赖另外一个任务时,只有等待被依赖的任务完成后,依赖的任务才能算完成,这是一种可靠的任务序列。 Either success succeeds, failure fails, and the state of the two tasks remains the same.

所谓异步是不需要等待被依赖的任务完成,只是通知被依赖的任务要完成什么工作,依赖的任务也立即执行,只要自己完成了整个任务就算完成了。 As to whether the dependent task is ultimately completed, the task that relies on it cannot be determined 所以它是不可靠的任务序列 .

1.2 Message Notifications

异步的概念和同步相对。 After a synchronous call is issued, the 调用者要一直等待返回消息(结果)通知后 subsequent execution can be performed; When an asynchronous procedure call is made, the caller cannot immediately get the return message (the result). 实际处理这个调用的部件在完成后,通过状态、通知和回调来通知调用者.

It is mentioned here that the executing parts and callers return the result in three ways: 状态、通知和回调 . Which notification mechanism to use, 依赖于执行部件的实现 unless the execution part provides multiple choices 否则不受调用者控制 .

  1. 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);

  2. 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.2 Scene metaphor

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

  1. Choose to wait in line;

  2. 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: 前者(排队等候)就是同步等待消息通知 , that is, I have been waiting for the bank to handle the business situation;

The second kind: 后者(等待别人通知)就是异步等待消息通知 . In asynchronous message processing, 等待消息通知者(在这个例子中就是等待办理业务的人)往往注册一个回调机制 when the waiting event is triggered by the trigger mechanism (the person here is the counter) through some mechanism (here is the number that is written on the small note, shouting number) to find the person waiting for the event.

2 blocking and non-blocking

阻塞和非阻塞这两个概念与程序(线程)等待消息通知(无所谓同步或者异步)时的状态有关。也就是说阻塞与非阻塞主要是程序(线程)等待消息通知时的状态角度来说的。

2.1 Concept Description

阻塞调用是指调用结果返回之前,当前线程会被挂起,一直处于等待消息通知,不能够执行其他业务。 Functions are returned only after the result is obtained.

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

    1. for synchronous calls, many times the current thread may still be active, but logically the current function does not return, at which point the thread may also process other messages . Also, here is the first extension:

      (a) if the thread waits for the current function to return, Other message processing is still being performed, which is called synchronous non-blocking;

      (b) If the thread waits for the current function to return, Instead of performing other message processing, it is in a pending wait state, which is called synchronous blocking;

      so there are two ways to implement synchronization: synchronous blocking, synchronous non-blocking, and there are two implementations of async: Asynchronous blocking, asynchronous non-blocking;

The concept of non-blocking and blocking corresponds to the 指在不能立刻得到结果之前,该函数不会阻塞当前线程,而会立刻返回 . Although the apparent non-blocking way can significantly improve the CPU utilization 但是也带了另外一种后果就是系统的线程切换增加 . 增加的CPU执行时间能不能补偿系统的切换成本需要好好评估.

2.2 Scene metaphor

Continue with the example above, whether it is queued or using a number to wait for notification, in the program, that is, 如果在这个等待的过程中,等待者除了等待消息通知之外不能做其它的事情,那么该机制就是阻塞的 the program has been blocked at the function call can not continue down execution.

On the contrary, 有的人喜欢在银行办理这些业务的时候一边打打电话发发短信一边等待,这样的状态就是非阻塞的 because he (the waiting person) does not block on this message notice, but while doing his own things while waiting.

But be aware that 同步非阻塞形式实际上是效率低下的 you need to look up while you're on the phone and see if the line is up to you. 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 异步非阻塞形式却没有这样的问题 , because the phone is you (waiting) things, and inform you is the counter (message trigger mechanism) things, The program does not switch back and forth between two different operations.

3 synchronous/Asynchronous vs. blocking/non-blocking
  1. Synchronous blocking Form

    The efficiency 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;

  2. asynchronous blocking form

    The asynchronous way to wait for the message to be triggered (notification) , that is, to receive a small note, if during this time he can not leave the bank to do other things, then it is clear 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.

    For example 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 .

  3. synchronous non-blocking form

    Imagine you're on the phone and you need to look up and see if the line is up to you. If you consider the location of the call and the observation queue as the two operations of the program, the program needs to switch back and forth between the two different behaviors. , the efficiency is imagined to be low.

    A lot of people write blocking read/write operations, But don't forget that you can set the O_NONBLOCK flag bit on FD so that you can turn the synchronization operation into a non-blocking .

  4. asynchronous non-blocking form

    more efficient,

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

    For example, the man 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.

    function returns immediately and is not blocked, and the previously registered callback function is called for processing when the event of interest is triggered .

Many people will confuse synchronization and blocking, I think 因为很多时候同步操作会以阻塞的形式表现出来 , 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. 但最根本是因为没有区分这两个概念, such as blocking the read/write operation, 其实是把消息通知机制和等待消息通知的状态结合在了一起 here 所关注的消息就是fd是否可读/写 , while 等待消息通知的状态则是对fd可读/写等待过程中程序(线程)的状态 . 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 with non-blocking, for example, 因为异步操作一般都不会在真正的IO操作处被阻塞 if you use the Select function 当select返回可读时再去read一般都不会被阻塞,而是在select函数调用处阻塞 .

4 Xiao Ming's story

The concept of the above is again a scene comb, the above has been clearly stated 同步/异步关注的是消息通知的机制,而阻塞/非阻塞关注的是程序(线程)等待消息通知时的状态 . 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.

  1. 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;

  2. 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"

  3. 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;

  4. 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"

同步/异步是“下载完成消息”通知的方式(机制),而阻塞/非阻塞则是在等待“下载完成消息”通知过程中的状态(能不能干其他任务) In other words, in different scenarios, there are four combinations of synchronous/asynchronous, blocking/non-blocking applications.

So, in summary, 同步和异步仅仅是关注的消息如何通知的机制,而阻塞与非阻塞关注的是等待消息通知时的状态 . In other words, 同步的情况下,是由处理消息者自己去等待消息是否被触发,而异步的情况下是由触发机制来通知处理消息者 so in the asynchronous mechanism, 处理消息者和触发机制之间就需要一个连接的桥梁 :

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.

最后,请大家注意理解“消息通知机制”和“等待消息通知时的状态”这两个概念,这是理解四个概念的关键所在。



Wen/Taubonren (Jane book author)
Original link: http://www.jianshu.com/p/aed6067eeac9
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

Talk about synchronous, asynchronous, blocking, and non-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.