The difference between blocking and non-blocking, the difference between synchronous and asynchronous

Reprinted from: https://www.cnblogs.com/orez88/articles/2513460.html
https://www.cnblogs.com/orez88/articles/2513460.html


The difference between blocking and non-blocking
Simply put:

Blocking means
you can’t finish it and you are not allowed to come back. Non-blocking means you do it first. Let me see if there is anything else, and let me know when it’s over.

Let's take the two most commonly used functions, send and recv... For
example, if you call the send function to send a certain Byte, the work done by send in the system is actually just to transfer data (Copy) to the output buffer of the TCP/IP protocol stack , Its successful execution does not mean that the data has been successfully sent out. If the TCP/IP protocol stack does not have enough available buffers to save the data you copied... At this time, the difference between blocking and non-blocking is reflected. : For blocking mode socket send function will not return until the system buffer has enough space to copy the data you want to send, and for non-blocking socket send will immediately return WSAEWOULDDBLOCK to tell the caller: " The sending operation is blocked!!! Do you think of a way to deal with it..."
For the recv function, the same is true. The internal working mechanism of the function is actually waiting for the receiving buffer of the TCP/IP protocol stack to notify it and saying: Hi, your data Coming. For a socket in blocking mode, if the receiving buffer of the TCP/IP protocol stack does not notify it of a result, it will never return: it consumes system resources... For a socket in non-blocking mode, the function will return immediately, and then Tell you: WSAEWOULDDBLOCK—"There is no data now, let's take a look back"

Extension:

In network programming, we often see four calling methods: synchronous, asynchronous, blocking and non-blocking. These methods are not easy to understand each other. The following is my understanding of these terms.

Synchronization The
so-called synchronization means that when a function call is issued, the call does not return until the result is not obtained. According to this definition, most functions are called synchronously (for example, sin, isdigit, etc.). But generally speaking, when we talk about synchronization and asynchronous, we specifically refer to tasks that require other components to collaborate or take a certain amount of time to complete. The most common example is SendMessage. This function sends a message to a certain window. This function does not return until the other party processes the message. After the other party has finished processing, the function returns the LRESULT value returned by the message processing function to the caller.

asynchronous

The concept of asynchronous is the opposite of synchronization. When an asynchronous procedure call is issued, the caller cannot get the result immediately. After the component that actually processes the call is completed, it notifies the caller through status, notification, and callback. Take the CAsycSocket class as an example (note that CSocket is derived from CAsyncSocket, but its function has been transformed from asynchronous to synchronous). When a client sends a connection request by calling the Connect function, the caller thread can immediately run down. When the connection is really established, the bottom layer of the socket will send a message to notify the object. It is mentioned that the execution component and the caller return results in three ways: status, notification and callback. Which one can be used depends on the implementation of the execution component, and unless the execution component provides multiple options, it is not controlled by the caller. If the execution component is notified by status, then the caller needs to check every certain time, and the efficiency is very low (some beginners of multi-threaded programming always like to use a loop to check the value of a variable, which is actually a Kind of very serious error). If you use the notification method, the efficiency is very high, because the execution component hardly needs to do additional operations. As for the callback function, it is not much different from notification.

Blocking A
blocking call means that the current thread will be suspended before the call result is returned. The function will only return after getting the result. Someone might equate blocking calls with synchronous calls, but they are actually 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. If there is no data in the buffer, this function will wait until there is data before returning. At this time, the current thread will continue to process various messages. If the main window and the calling function are in the same thread, unless you call it in a special interface operation function, the main interface should still be refreshable. Another function recv of socket receiving data is an example of blocking call. When the socket is working in blocking mode, if this function is called without data, the current thread will be suspended until there is data.

Non
- blocking Non - blocking corresponds to the concept of blocking, which means that the function will not block the current thread before getting the result immediately, but will return immediately.

Object blocking mode and blocking function call

Whether the object is in blocking mode has a strong correlation with whether the function is blocked or not, but there is no one-to-one correspondence. There can be a non-blocking calling method on the blocking object. We can poll the state through a certain API, and call the blocking function at the appropriate time to avoid blocking. For non-blocking objects, calling special functions can also enter blocking calls. The function select is an example of this.

Guess you like

Origin blog.csdn.net/wx_assa/article/details/107704697