synchronous, asynchronous, blocking, and non-blocking differences

Source: Internet
Author: User

Simply put:

Blocking is to do not come back, has been in the waiting, until the completion of the process to return;
Non-blocking is the first thing you do, I'll see if there's anything else. No, a find things are stuck, immediately report the leadership.

Let's take the most commonly used send and recv two functions ...
For example, if you call the Send function to send a certain byte, the work done inside the system is actually just transferring data (Copy) to the output buffer of the TCP/IP stack, it does not mean that the data has been successfully sent out, if tcp/ The IP stack does not have enough buffers available to hold the data you copied ... This is the difference between blocking and non-blocking: the socket send function for blocking mode will not return until the system buffer has enough space to copy the data you want to send. For a nonblocking socket, send returns immediately wsaewoulddblock tells the caller, "the send operation is blocked!!! You have to deal with it ... "

For the RECV function, the internal working mechanism of the function is actually waiting for the TCP/IP protocol stack to notify the receiving buffer that it says: Hi, your data is coming. For a blocking mode socket, if the TCP/IP protocol stack's receive buffer does not notify a result to it, it never returns: Consuming system resources .... For a non-blocking socket, the function returns immediately, and then tells you: Wsaewoulddblock---"No data now, look back."

Extended:

In the network programming, we often see synchronous, asynchronous, blocking and non-blocking four ways to call. These ways are not well understood in each other's concepts. Here is my understanding of these terms.

1. Synchronization

The so-called synchronization is that when a function call is made, the call does not return until the result is obtained. By this definition, the vast majority of functions are synchronous calls (such as sin, isdigit, etc.). But generally speaking, we are talking about synchronous and asynchronous tasks, especially those that require other components to collaborate or need to be done in a certain amount of time. The most common example is SendMessage. The function sends a message to a window that does not return until the other party finishes processing the message. When the other party finishes processing, the function returns the LRESULT value returned by the message handler function to the caller.

2. Asynchronous

Asynchronous concepts and synchronization are relative. When an asynchronous procedure call is made, the caller cannot get the result immediately. The part that actually handles the call notifies the caller via status, notification, and callback after completion. Take the Casycsocket class as an example (note that CSocket derives from CAsyncSocket, but the function has been converted from asynchronous to synchronous), and when a client makes a connection request by calling the Connect function, the caller thread can run down immediately. When the connection is actually set up, the bottom of the socket sends a message informing the object. It is mentioned here that the executing parts and callers return results in three ways: status, notifications, and callbacks. Which one can be used depends on the implementation of the execution part, which is not controlled by the caller unless the execution part provides multiple choices. 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). 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.

3, 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. One might equate blocking calls with synchronous invocations, in fact they are different. For synchronous calls, many times the current thread is still active, but logically the current function does not return. For example, we call the receive function in CSocket, and if there is no data in the buffer, the function waits until there is data to return. At this point, the current thread will continue to process a wide variety of messages. If the main window and the calling function are in the same thread, the main interface should be refreshed unless you call in a special interface action function. Another function that the socket receives data 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.

4, 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.

Blocking mode and blocking function calls for objects

Whether the object is in blocking mode and if the function is not a blocking call has a strong correlation, but not one by one corresponds. Blocking objects can have non-blocking calls, we can use a certain API to poll the state, the appropriate time to call the blocking function, you can avoid blocking. For non-blocking objects, calling a special function can also enter a blocking call. The function Select is an example of this.

synchronous, asynchronous, blocking, and non-blocking differences

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.