Talk about synchronous, asynchronous, blocking, and non-blocking

Source: Internet
Author: User
Tags ming

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. 对于同步调用来说,很多时候当前线程可能还是激活的,只是从逻辑上当前函数没有返回而已,此时,这个线程可能也会处理其他的消息。 Also, here's the first extension:

      (a) If the thread is still performing other message processing while waiting for the current function to return, then this is called synchronous non-blocking;

      (b) If the 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;

      所以同步的实现方式会有两种:同步阻塞、同步非阻塞;同理,异步也会有两种实现:异步阻塞、异步非阻塞;

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

    If the person in the bank waits for a business, that is, 采用的是异步的方式去等待消息被触发(通知) a small note, if he cannot leave the bank to do other things during this time, it is obvious that this person is blocked on the waiting operation;

    异步操作是可以被阻塞住的,只不过它不是在处理消息时阻塞,而是在等待消息通知时被阻塞。

    For example, the Select function, if the last timeout parameter passed in is null 那么如果所关注的事件没有一个被触发,程序就会一直阻塞在这个select 调用处 .

  3. 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 queuing position as a program of two operations, it 这个程序需要在这两种不同的行为之间来回的切换 is conceivable that the efficiency is low.

    Many people will write blocking Read/write operations 但是别忘了可以对fd设置O_NONBLOCK 标志位,这样就可以将同步操作变成非阻塞的了 .

  4. Asynchronous non-blocking form

    Higher efficiency,

    Because the phone is your (waiting) thing, and inform you is the counter (message trigger mechanism) things, 程序没有在两种不同的操作中来回切换 .

    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 are using asynchronous non-blocking scenarios, such as the operations of the Aio_* group, when a aio_read operation is initiated 函数会马上返回不会被阻塞,当所关注的事件被触发时会调用之前注册的回调函数进行处理 .

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.