What exactly is the difference between synchronous/asynchronous and blocking/non-blocking?
怪我咯
怪我咯 2017-05-17 10:08:42
0
4
893

As far as I understand, synchronization/blocking are the same concept. The client is waiting for a receipt from the server. If the server does not return a receipt, the client will not proceed; asynchronous/non-blocking means that the client does not wait for the service. If you receive the receipt from the client, go straight down. After the server-side processing is completed, it will call the asynchronous callback function to notify the client.

But specifically, I can’t tell the difference between synchronization and blocking, and asynchronous and non-blocking. Can anyone clarify the confusion?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(4)
習慣沉默

Recommend a blog post to talk about the five IO models of Linux. It is very well written. Let me tell you these briefly.
First of all, only synchronization has so-called blocking and non-blocking, asynchronous does not. The common misunderstanding is that we think asynchronous means non-blocking, but this is not the case. The difference between synchronization and asynchronousness here is that whether there is any blocking in the "whole process" of a network IO or disk IO is the entire process.
Take a read system call as an example. As a user thread, when you initiate a read system call, it can be divided into two operations,

  • The first is data reading: reading data from disk to kernel space. We all know that read is a system call and user-level threads cannot operate it. It can only be handed over to kernel threads for processing, and kernel threads must first Find the data and read it into kernel space.

  • The second is data copy: reading data from kernel space to user space. Then the user thread can use this data.

So simply put,

  • Synchronization means that the above two processes are blocked, and your user thread has been waiting.

  • Non-blocking means that you do not block the first process above, but the user thread must constantly ask the OS whether the data has been copied from the disk to the internal space. If the copy is completed, it will block during the data copying process. So all synchronization processes are blocking in the second phase, although this is a non-blocking call.

  • Multiplexing: Like non-blocking, it is also blocking in the second stage, but in the first stage, you no longer ask the operating system yourself, but are uniformly handed over to a kernel thread for processing (poll is implemented on Linux , and an improved version of epoll), when your data reading is completed, this thread sends a signal to the user thread that originally initiated the system call, and then the user thread enters blocking and starts data copying.

  • Asynchronous: The above two processes are non-blocking.

The above is just a brief description, I hope it helps

PHPzhong

The concept of synchronization and asynchronous mainly describes IO aspects. In short, the main difference between synchronization and asynchronous is the way to notify the calling process or thread. Immediate return notification is synchronous, while notification through registered callback is asynchronous. Blocking and non-blocking mainly describe the return situation of function calls. A function that returns immediately is non-blocking, and a function that is suspended is blocking.

To give a simple analogy, if you go to a restaurant to order food.

Synchronous blocking

You tell the ordering clerk that you want Taiwanese beef noodles. When the ordering clerk hears it, he goes to the kitchen and takes a while to bring it out to you. During this period of time, you were waiting stupidly at the front desk without any reply or doing anything (blocking). You did not receive any reply after placing the order. The process of waiting is synchronous communication.

Synchronous and non-blocking

You told the ordering clerk that you wanted a Lanzhou beef noodles, and the ordering clerk replied that it would take five minutes. Then you think for a moment, you can check the posts in five minutes and do other things to relieve your boredom. But soon you get hungry, and you ask every minute if it's okay, but the reply you get is that it's not okay yet, and you don't get a face to face until 5 minutes later. While waiting, you are not idle and can do other things. This is non-blocking. Since you are still actively asking for the results and waiting for the orderer's reply, this is still synchronous.

Asynchronous blocking and non-blocking

The so-called asynchronous means that you do not need to actively ask for the results, but register a callback function. That is, after you place your order, the ordering clerk gives you a number. There is a machine next to you. When it's your turn, the machine will call your number. The process of notifying you is called asynchronous. If you sit aside and do nothing, then you are in a blocked state. If you are browsing the webpage on the side, it is non-blocking.

So the difference between synchronous and asynchronous mainly lies in the notification method of the message. Blocking and non-blocking lie in the status of the function call when waiting for notification, that is, whether it is suspended, so that the current thread or process, and whether it can continue to do other things.

There is usually a coroutine way to achieve asynchronous non-blocking. That is, when a function call encounters IO, after registering the callback function, it suspends and returns. Because it returns, it is non-blocking. Then when the IO is completed, the callback function notifies the suspended function to wake up, which is asynchronous.

phpcn_u1582

Generally speaking, blocking and non-blocking refer to whether the IO call returns immediately (non-blocking) or waits for completion before returning (blocking). Synchronization and asynchronousness are broad concepts and are manifestations of blocking and non-blocking.

给我你的怀抱

Actually, you understand it correctly. Synchronization means that there will be no inconsistency in data, and single-threading is executed sequentially. Out of synchronization means that the data is not uniform. For example, when there are multiple threads, the data used by this thread may be modified by another thread, causing the data to be out of sync. Blocking and non-blocking refer to whether to wait for the function to return when the thread is running. If it is a single thread, it will always wait. If it is multi-threaded, it will not wait for downward execution. At this time, desynchronization is prone to occur.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template