Blocking, non-blocking, synchronous, asynchronous Analysis

Source: Internet
Author: User

In network programming, we often encounter the concepts of blocking, non-blocking, synchronous, and asynchronous. Many people are very confused. Now let's talk to everyone about my understanding of the two concepts.

First, let's talk about blocking and non-blocking. This is mainly related to the status when the program waits for a message.

1. Blocking

The program will block a function without executing it. Just like hanging it there, all other services will not be executed, so it will not be executed until the message arrives.

2. Non-blocking

The program does not block a function. It does not wait for the message to arrive, returns immediately, and runs down.

For example, the system will allocate a sending cache for the send in TCP. Assume that the current total cache size is 1000 ., the cache area already contains 500 data records. At this time, send is called to send 1000 bytes of data. In blocking mode, send will first put the most data into the cache, until the cache is full, the program will block it until all the data is sent out. If it is in non-blocking mode, send will put the most data into the cache first, and then return immediately. The remaining data will be sent back and will not be stuck in the send function.

The two concepts of synchronization and Asynchronization are actually related to the Message notification mechanism.

1. Synchronization

Send a message and wait until the message is processed. Such as sendmessage () in MFC (),

2. asynchronous

Send a message. If you do not wait for the message to be processed, execute the message and notify you of the completion of the message through a specific interface or event. For example, postmessage () in MFC ()

After a rough introduction to the above two concepts, we may ask what is the difference between blocking and synchronization, non-blocking and asynchronous. Use Cases.

For example, if you want to go to a bank now, you can select two methods: Ticket receiving wait and waiting in line, at this point, if you cannot do anything while waiting, you will be blocked. If you can still call, drink, read, but you have to look up from time to see the number of people in front of you, when it is your turn, do not miss the handling, then you are in a synchronous state.

That is to say, blocking means that the program is hanging there, and nothing else can be done until the current event is returned, and the synchronization has to wait for the message again. At the same time, the execution of other services is not affected, it is reflected in the business processing of other threads in the program.

If you wait for a ticket, you just need to sit in a chair and then you can do what you want to do, such as listening to songs, reading books, making phone calls, the window will automatically call you, and you will be in a non-blocking status.

If you still feel uncomfortable and want to go out for a stroll at this time, you can talk to the staff in the hall where I am going and will be there soon, let me know where you are, and then you can go out and do what you want until the staff calls you. In this case, you are in the asynchronous state. The combination of the two is the so-called asynchronous non-blocking mode, which is often used in network programming due to high efficiency.

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.