Blocking versus non-blocking, synchronous and asynchronous

Source: Internet
Author: User

1 Lao Zhang put the kettle on the fire and conforming the water. ( synchronous blocking )

Lao Zhang thinks he's a little silly.

2 Lao Zhang put the kettle on the fire, go to the living room to watch TV, and occasionally go to the kitchen to see if the water is open. ( synchronous non-blocking )

Lao Zhang still feel a little silly, then become high-end, bought a will ring flute of that kind of kettle. After the water is open, you can make a loud noise.

3 Old Zhang put the Kettle on the fire and conforming the water. ( asynchronous blocking )

Lao Zhang thinks such a silly meaning is not very important

4 Old Zhang put the Kettle on the fire, went to the living room to watch TV, the kettle rang no longer to see it, rang again to get the pot. ( asynchronous non-blocking )

Lao Zhang thought he was smart.

The so-called synchronous asynchronous, just for the kettle.

Ordinary kettle, synchronous; kettle, asynchronous.

Although all can work, but the kettle can be completed after their own, the old Zhang Water opened. This is not an ordinary kettle.

Synchronization can only allow callers to poll themselves (in case 2), resulting in inefficient old Zhang.

The so-called blocking non-blocking, only for the old Zhang.

The old Zhang of conforming, blocking; Old Zhang watching TV, non-blocking.

Situation 1 and situation 3 old Zhang is blocked, the daughter-in-law shouted he did not know. Although the 3-ring kettle is asynchronous, it does not make much sense to conforming's old Zhang. So asynchronous is generally used in conjunction with non-blocking, in order to play an asynchronous effect.

Programmers write programs, and in many cases, Lao Zhang and the kettle are involved, that is, the caller and the callee. Four combinations of modes have their own strengths.

When you get to the bank,
You can wait in front of the ATM machine--(waiting in line) is a synchronous wait message.
. You can go to the lobby to pick up the number and wait until it comes to my number.
The people at the counter will tell me it's my turn to handle the business. --(Waiting for someone to be notified) is an asynchronous wait message.

A. Blocking
A blocking call means that the current thread is suspended until the call results are returned. Functions are returned only after the result is obtained.
Someone might equate blocking calls with synchronous calls, and in fact he is different.
For synchronous calls, many times the current thread is still active, but logically the current function does not return.

The socket receive data function recv is an example of a blocking call.
When the socket is working in blocking mode, if the function is called without data, the current thread is suspended until there is data.

B. Non-blocking
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.

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 the read/write operation that does not set the O_NONBLOCK flag bit to FD,

Asynchronous blocking form:
if the person in the bank waits for a business to use an asynchronous way to wait for the message to be triggered, a small note is brought
If during this time he could not leave the bank to do anything else, it was clear that the man was 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 the message to be triggered.
like the Select function,
if the last timeout parameter passed in is null, then if none of the events of interest are triggered,
The program will always block at this select call.

synchronous non-blocking form:
is actually inefficient,
imagine you're on the phone, and you need to look up and see if the team's on your side.
If you consider the location of the telephone and the observation line as the two operations of the program,
This program needs to switch back and forth between these two different behaviors, the efficiency can be imagined to be low;

Many people will write blocking read/write operations,
But don't forget that you can set the O_NONBLOCK flag bit to FD so that you can turn the synchronization operation into a non-blocking one;

asynchronous non-blocking form:
Higher efficiency,
because the phone is your (waiting) thing, and notice you is the counter (message trigger mechanism) things,
The program does not switch back and forth between 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 that when it comes to my number, let me know when I get there (register a callback function),
then he is not blocked on this waiting operation, and naturally this is the asynchronous + non-blocking way.

If you are using asynchronous non-blocking scenarios,
For example, the operation of the Aio_* group, when initiating a aio_read operation, the function will immediately return will not be blocked,
when the event of interest is triggered, the previously registered callback function is called for processing.

Blocking versus non-blocking, synchronous and asynchronous

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.