Synchronous/asynchronous and blocking/non-blocking

Source: Internet
Author: User

I. synchronous and asynchronous
Synchronous/asynchronous, which are notifications of messages

1. Concepts
A. Synchronization
The so-called synchronization means that when a function call is sent, the call will not return until the result is not obtained.

According to this definition, most functions are called synchronously (such as sin isdigit ).
But in general, when we talk about synchronization and Asynchronization, we are referring to the tasks that require the collaboration of other components or a certain amount of time.
The most common example is sendmessage.
This function sends a message to a window. This function does not return a message before the recipient finishes processing the message.
After the other party completes processing, the function returns the value returned by the message processing function to the caller.


B. asynchronous
Asynchronous concept and synchronization relative.
When an asynchronous process is called, the caller does not immediately obtain the result.
The actual processing of this call part is after the call is issued,
Notify the caller by status or notification, or process the call through the callback function.

Taking socket as an example,
When a client sends a connection request by calling the connect function, the caller's thread can continue to run immediately without waiting for the result.
After the connection is established, the socket underlying layer sends a message to notify the object.

C. Three return methods
The execution part and caller can return results in three ways:
A. status,
B. Notifications,
C. callback function.

Which one can be used depends on the implementation of the execution part, unless the execution part provides multiple options, it is not controlled by the caller.

A. if the execution part is notified by status,
Then the caller needs to check every certain time, and the efficiency is very low.
Some new programmers prefer to use a loop to check the value of a variable. This is actually a very serious error.

B. If the notification method is used,
High efficiency, because the execution of components almost do not require additional operations.

C. As for the callback function,
There is no much difference from notifications.


2. Examples
To understand these two concepts, you can use a bank to handle business operations (you can get money or save money) as a metaphor:
After arriving at the bank,
. You can wait in queue before going to the ATM. (waiting in queue) means waiting for messages synchronously.
. You can get the number in the hall,
The person at the counter will inform me that it is my turn to handle the business. -- (wait for others' notifications) is to wait for messages asynchronously.

In the Asynchronous Message notification mechanism,
The waiting message owner (in this example, the person waiting for processing the business) often registers a callback mechanism,
When a waiting event is triggered, it is triggered by a trigger mechanism (the person at the counter here) through a mechanism (here it is the number written on the small note)
Find the person waiting for the event.

In Io multiplexing mechanisms such as select/poll, FD is used,
When a message is triggered, the trigger mechanism uses FD to find the processing function for processing the FD.

3. In the actual program,
Synchronous Message notification mechanism: it is like a simple read/write operation. They need to wait until these two operations are successful before they can be returned;
Synchronization means that the message handler waits for the message to be triggered;
Asynchronous Message notification mechanism: multiplexing Io operations, such as select/poll,
When a message of interest is triggered, it is triggered by a message trigger mechanism notification to process the message.
Asynchronous: a trigger mechanism is used to notify the message handler;


Back to the example above,
It's your turn to handle the business. This is the message you pay attention,
The processing of this message is what business is to handle,
The two are different.

In actual Io operations: the message concerned is whether the FD can be read and written,
The processing of messages is to read and write the FD.

Synchronous/asynchronous only focuses on how to notify messages, but they do not care about how to process messages,
For example, the bank only notifies you that it is your turn to handle the business,
They do not know what business (saving money or taking money) they handle.

Ii. Blocking and non-blocking
Blocking/non-blocking. They are the status when the program is waiting for the message (it doesn't matter whether it is synchronous or asynchronous.

1. Concepts
A. Blocking
Blocking call means that the current thread will be suspended before the call result is returned. The function is returned only after the result is obtained.
Some people may equate blocking calls with synchronous calls. In fact, they are different.
For synchronous calls, the current thread is still activated in many cases, but the current function does not return logically.

The socket receiving data function Recv is an example of blocking calls.
When the socket works in blocking mode, if the function is called without data, the current thread will be suspended until there is data.

B. Non-blocking
The concept of non-blocking corresponds to blocking, which means that the function will not block the current thread and return immediately before the result cannot be obtained immediately.

C. Object blocking mode and function calling
Whether the object is in blocking mode is highly correlated with whether the function is blocked or not, but it is not one-to-one.


Blocking objects can have non-blocking calling methods. We can use certain APIs to poll the status,
When appropriate, call the blocking function to avoid blocking.
For non-blocking objects, calling special functions can also be blocked. The select function is an example.

2. Examples
Continue with the example above,
Whether waiting in queue or waiting for notification by phone number,
If, during the waiting process,
The waiting person cannot do anything other than waiting for messages, so this mechanism is blocked,
It is manifested in the program, that is, the program has been blocked and cannot continue to be executed in the function call.
On the contrary, some people like to call and send text messages while waiting for these services in the bank. This status is non-blocking,
It is because he (the waiting person) does not block the Message notification, but waits while doing his own thing.

3. obfuscated points
Many people will also confuse asynchronous and non-blocking,
Because asynchronous operations are generally not blocked in real Io operations,
For example, if the select function is used, it will not be blocked when the select function returns readable data.
It is like when your number is arranged, it is generally because you have no one before, so you will not be blocked when you go to the counter to handle the business.
It can be seen that synchronous/asynchronous and blocking/non-blocking are two different concepts, they can coexist and combine,

Many people confuse synchronization and blocking because they do not distinguish between them,
For example, in a blocked read/write operation, message notifications and processing messages are combined,
The message concerned here is whether the FD is readable/written, while the message processing is the FD read/write.
When we set this FD to non-blocking, the read/write operation will not block it while waiting for message notifications,
If FD cannot be read or written, the operation returns immediately.


Iv. synchronous/asynchronous and blocking/non-blocking Combination Analysis
_______ Blocking ________________ non-blocking _____
Synchronization | synchronization blocking synchronization non-blocking
Asynchronous | asynchronous blocking asynchronous non-blocking

Synchronous blocking:
The efficiency is the lowest,
Taking the above example as an example, you can concentrate on queuing and do nothing else.

In the actual program
It is the read/write operation that does not set the o_nonblock flag for FD,

Asynchronous blocking:
If the bank waits for the business to be handled in an asynchronous way to wait for the message to be triggered, that is, a small piece of paper is received,
If he cannot leave the bank to do other things during this time, it is obvious that this person is blocked in the waiting operation;


Asynchronous operations can be blocked, except that they are not blocked when processing a message, but are blocked when the message is triggered.
For example, the Select function,
If the input last timeout parameter is null, if none of the following events is triggered,
The program will always block this select call.

Non-blocking synchronous mode:
It is actually inefficient,
Imagine you still need to look up and see if the team has reached you while you are on the phone,
If you think of the call and the position of the observation queue as two operations of the program,
This program needs to switch back and forth between the two different behaviors, and the efficiency can be imagined as low;

Many may write blocked read/write operations,
But don't forget to set the o_nonblock flag for FD so that the synchronization operation can be turned into non-blocking;

Asynchronous non-blocking mode:
Higher efficiency,
Because the call is a matter for you (waiting for someone), and the notification to you is a matter for the counter (message trigger mechanism,
The program does not switch back and forth in two different operations.

For example, this person suddenly finds himself addicted to smoking cigarettes and needs to go out to smoke cigarettes,
So he told the manager in the lobby to inform me (register a callback function) when the number is reached ),
Then he is not blocked in the waiting operation. Naturally, this is the asynchronous + non-blocking method.

If asynchronous non-blocking is used,
For example, for AIO _ * Group operations, when an aio_read operation is initiated, the function will return immediately and will not be blocked,
When a concerned event is triggered, the previously registered callback function will be called for processing,

Synchronous/asynchronous and blocking/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.