Windows I/O model, synchronous/asynchronous, blocking/non-blocking (reprint)

Source: Internet
Author: User

Reprinted from: http://www.cppblog.com/tx7do/articles/5954.html

Synchronous
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.

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.

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

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.


There are many ways to implement Asynchrony in Winsock, and the IO model of Winsock has the following six types
One: Select model
Two: WSAAsyncSelect model
Three: WSAEventSelect model
Four: Overlapped I/O event notification model
V: Overlapped I/O completion routine model
VI: IOCP model
From one to six more and more advanced, more efficient and more complex to achieve.


I have seen some metaphors on the internet to illustrate these models well, and to cite them here.
Lao Chen had a daughter who worked outside and could not come back often, and she contacted her by letter. Their letter will be delivered to their mail box by the postman.
One: Select model

Lao Chen wanted to see his daughter's letter very much. So that he went downstairs every 10 minutes to check the mailbox to see if he had a daughter's letter ~~~~~
In this case, "go downstairs to check the mailbox" and then back upstairs to delay the old Chen too much time, so that Lao Chen can not do other work.

Two: WSAAsyncSelect model

Later, Lao Chen used the new mailbox of Microsoft Corporation. This kind of mailbox is very advanced, once the new letter in the mailbox, Gates will call old Chen: Hello, Grandpa, you have a new letter! From then on, Lao Chen no longer have to check the mailbox frequently, the tooth also does not ache, you look quasi, the blue sky ... No, Microsoft ~~~~~~~~

Three: WSAEventSelect model

Later, Microsoft's mailbox is very popular, the number of people who buy Microsoft mailbox is counted in millions ... So that Gates 24 hours a day to call customers, tired backache back pain, drinking ant force God is not good to make ~~~~~~
Microsoft has improved their mailbox by adding an add-on device to the customer's home that monitors the customer's mailbox, and whenever a new letter arrives, the device sends a "new letter arrival" to remind Lao Chen to collect the letter. Finally, gates can sleep.

Four: Overlapped I/O event notification model

Later, Microsoft through the investigation found that Lao Chen does not like to go downstairs to send and receive letters, because the upper and lower floors are actually a waste of time. So Microsoft is improving their mailbox again. New mailboxes use more advanced technology, as long as users tell Microsoft their home in the number of floors, the new mailbox will send the letter directly to the user's home, and then tell the user, your letter has been put in your home! Lao Chen is very happy because he doesn't have to send and receive letters himself!

V: Overlapped I/O completion routine model

Old Chen received a new letter, the general procedure is: Open the envelope----out the stationery----Read the letter----reply to the letter ... In order to further alleviate the user burden, Microsoft has developed a new technology: Users just tell Microsoft about the steps of the letter, Microsoft Mailbox will follow these steps to process the letter, no longer require users to personally eagerness/read/reply! Lao Chen finally had a petty bourgeoisie life!

VI: IOCP model

Microsoft mailbox seems to be perfect, old Chen is also very satisfied. But in some big companies the situation is completely different! These big companies have tens of thousands of mailboxes, each with hundreds of letters to deal with, so Microsoft mailbox often crashes due to overloaded operation! Need to reboot! Microsoft had to make a killer ...
Microsoft sent a super robot named "Completion Port" to each big company to get the robot to handle the letters!


In fact, each of the above models have advantages, according to the needs of the program and appropriate selection of the appropriate model, the previous three models of efficiency has been relatively high, the implementation is not small, a lot of general network programs are used in the first three models, only to the network requirements of some servers will consider using the latter models. MFC in the CAsyncSocket class is used in the WSAAsyncSelect model, electric donkey is also used in this, but in the search for the corresponding socket is optimized, find faster, in the gridcast is WSAEventSelect model, wait.

Windows I/O model, synchronous/asynchronous, blocking/non-blocking (reprint)

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.