Scientific knowledge: synchronous, asynchronous, blocking and non-blocking differences _ other synthesis

Source: Internet
Author: User

To put it more simply:

Obstruction is not to do not come back, has been waiting until the matter is completed before returning;
If you do it first, I'll see if there's anything else. No, as soon as I found out something was stuck, I immediately reported to the leader.

Let's take the most commonly used send and recv two functions ...
For example, you call the Send function to send a certain byte, in the system sent to do the work is actually just the data transfer (COPY) to the TCP/IP protocol stack output buffer, and its success does not mean that the data has been successfully sent out, if the tcp/ The IP protocol stack does not have enough buffers available to hold the data you copy. This shows 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 return the data copy you are sending. For a non-blocking socket, send returns immediately wsaewoulddblock tells the caller, "the send operation is blocked!!! You find a way 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 be notified by the receiving buffer: Hi, your data is coming. For blocking-mode sockets, if the TCP/IP protocol stack's receive buffer does not notify a result to it it does not return: Consumes the system resources .... For a non-blocking-mode socket The function will return immediately, and then tell you: Wsaewoulddblock---"There is no data now, look back."

Extended:

When doing network programming, we often see four ways of calling synchronous, asynchronous, blocking, and non-blocking. These approaches are not well understood by each other. Here is my understanding of these terms.

1, synchronization

Synchronization is when a function call is issued, and the call does not return until the result is obtained. By this definition, most functions are synchronous calls (such as sin, isdigit, etc.). But generally speaking, when we say synchronous, asynchronous, especially those who need other parts to collaborate or need a certain amount of time to complete the task. The most common example is SendMessage. The function sends a message to a window that does not return until the other person finishes processing the message. This function returns the LRESULT value returned by the message handler function to the caller when the other party has finished processing it.

2, asynchronous

Asynchronous concepts and synchronizations are relative. When an asynchronous procedure call is issued, the caller cannot immediately obtain the result. When the part that actually handles this call is completed, the caller is notified by status, notification, and callback. Take the Casycsocket class as an example (note that CSocket derives from CAsyncSocket, but the function has been converted from asynchronous to synchronous), and the caller thread can run down immediately after a client makes a connection request by calling the Connect function. When the connection is really established, the bottom of the socket sends a message to notify the object. It is mentioned here that the execution part and the caller return results in three ways: status, notification, and callback. You can use which depends on the implementation of the execution part, unless the execution part provides a variety of choices that are not controlled by the caller. If the executing part is notified by state, then the caller needs to check every time, and 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 the execution part requires little extra action. As for the callback function, there is not much difference between the notice and the notification.
3. Blocking

A blocking call is the current thread is suspended until the call result returns. The function is returned only after the result is obtained. Someone might equate a blocking call with a synchronous call, in fact they are different. For a synchronous call, 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 the data is returned. 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, unless you call in a special interface operation function, the main interface should still be refreshed. Another function of the socket receiving 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 before the result is immediately available.

Blocking mode and blocking function calls for objects

The object is in blocking mode and the function is not a blocking call has a strong correlation, but it is not one by one corresponding. Blocking objects can have non-blocking calls, we can through a certain API to polling state, at the appropriate time to call the blocking function, you can avoid blocking. For Non-blocking objects, calling special functions can also go into blocking calls. The function Select is one such example.

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.