Windows I/O model, synchronous/asynchronous, blocking/non-blocking

Source: Internet
Author: User
Tags microsoft mail

Synchronization

The so-called synchronization means that when a function call is sent, the call will not return until the result is not obtained. According to this definition, most functions are called synchronously (such as sin and isdigit ). But in general, when we talk about synchronization and Asynchronization, we are referring to the tasks that require the collaboration of other components or a certain amount of time. The most common example is sendmessage. This function sends a message to a window. This function does not return a message before the recipient finishes processing the message. After the other party completes processing, the function returns the lresult value returned by the message processing function to the caller.

Asynchronous

Asynchronous concept and synchronization relative. When an asynchronous process is called, the caller cannot obtain the result immediately. After the call is completed, the caller is notified by status, notification, and callback. Take the casycsocket class as an example (note that csocket is derived from casyncsocket, but the function has been converted from Asynchronous to synchronous). When a client sends a connection request by calling the connect function, the caller's thread can run down immediately. After the connection is established, the socket underlying layer sends a message to notify the object. The Execution Component and caller can return results in three ways: Status, notification, and callback. Which one can be used depends on the implementation of the execution part, unless the execution part provides multiple options, it is not controlled by the caller. If the Execution Department
When the status is used for notification, the caller needs to check each time and the efficiency is very low (some beginners who are new to multi-thread programming prefer to use a loop to check the value of a variable, this is actually a very serious error ). If you use the notification method, the efficiency is very high, because the execution of components almost do not need to do additional operations. As for the callback function, there is actually no much difference with the notification.

Blocking

Blocking call means that the current thread will be suspended before the call result is returned. The function is returned only after the result is obtained. Some people may equate blocking calls with synchronous calls. In fact, they are different. For the same-step call, the current thread is still activated in many cases, but the current function does not return logically. For example, we call the receive function in csocket. If there is no data in the buffer zone, this function will wait until data is returned. At this time, the current thread will continue to process a variety of messages. If the main window and the called function are in the same thread, the main interface should be refreshed unless you call the function in special interface operations. Another function Recv used by socket to receive data is an example of blocking calls. When the socket works in blocking mode,
If this function is called without data, the current thread will be suspended until data exists.

Non-blocking

The concept of non-blocking corresponds to blocking, which means that the function will not block the current thread and return immediately before the result cannot be obtained immediately.

Object blocking mode and function calling

Whether the object is in blocking mode is highly correlated with whether the function is blocked or not, but it is not one-to-one. Blocking objects can have non-blocking call methods. We can use certain APIs to poll the status and call the blocking function when appropriate to avoid blocking. For non-blocking objects, calling special functions can also be blocked. The select function is an example.


There are many asynchronous methods in Winsock. There are the following six Io models for Winsock:
I. Select model
Ii. wsaasyncselect Model
Iii. wsaeventselect Model
4. overlapped I/O Event Notification Model
V. overlapped I/O completion routine model
Vi. iocp Model

From the first to the sixth, more advanced, more efficient, and more complex implementation.

I have seen some metaphors on the Internet to describe these models very well. I will reference them here.
Old Chen has a daughter who works in a field and cannot come back frequently. She and she contact him by letter. Their mail will be delivered to their mailbox by the postman.
I. Select model

Chen wanted to hear from his daughter. So that he goes downstairs every 10 minutes to check whether he has a daughter's email ~~~~~
In this case, "go downstairs to check the mailbox" and return to the upstairs, which delayed Lao Chen too much time, so that he could not do other work.

Ii. wsaasyncselect Model

Later, Chen used a new Microsoft mailbox. This kind of mailbox is very advanced. Once there are new letters in the mailbox, Gates will call Chen: Hello, Lord, you have new letters! From then on, Old Chen no longer has to go up and down frequently to check the mailbox, and his teeth do not hurt. You can look at it, blue sky ...... No, Microsoft ~~~~~~~~

Iii. wsaeventselect Model

Later, Microsoft's mailbox became very popular. The number of people who bought Microsoft Mail was counted as one million ...... as a result, Gates calls the customer 24 hours a day, causing a sore waist and backache ~~~~~~
Microsoft improved their mailbox: by adding an additional device to the customer's home, the device monitors the customer's mailbox, whenever new mail comes, this device will send a "new letter arrives" to remind old Chen to receive the mail. Gates can finally go to bed.

4. overlapped I/O Event Notification Model

Later, Microsoft found through a survey that Chen did not like sending and receiving letters from the upper and lower floors, because the upper and lower floors were a waste of time. So Microsoft improved their mailbox again. The new mailbox uses more advanced technologies. As long as the user tells Microsoft the number of its home on the floor, the new mailbox will send the mail directly to the user's home and then tell the user, your letter has been placed in your home! Old Chen is very happy because he no longer needs to send and receive emails in person!

V. overlapped I/O completion routine model

After Chen receives a new letter, the general procedure is: open the envelope, pull out the letter paper, read the letter, and reply to the letter ...... to further reduce the burden on users, Microsoft has developed a new technology: as long as you tell Microsoft the operation steps on the mail, Microsoft Mail will follow these steps to process the mail, you no longer need to personally split/read/reply! Chen has finally lived a petty asset!

Vi. iocp Model

Microsoft mailbox seems perfect, and Chen is also very satisfied. But in some big companies, the situation is completely different! These large companies have tens of thousands of mailboxes, and hundreds of mails need to be processed every second, so that Microsoft mailboxes often crash due to overload! Need to restart! Microsoft had to launch a killer ......
Microsoft assigned a super robot named "completion port" to every big company to process those letters!


In fact, each of the above models has advantages. to select an appropriate model based on program requirements, the efficiency of the first three models is already relatively high. Isn't it very easy to implement, many general network programs use the first three models. Only servers with high network requirements will consider the models below. The casyncsocket class in MFC uses the wsaasyncselect model, which is also used in the plug-in. However, it is optimized when looking for the corresponding socket to make the search faster. In gridcast, The wsaeventselect model is used, wait.

Reprinted: Click to open the link

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.