Asynchronous IO model and overlapped structure

Source: Internet
Author: User

. NET overlapped class asynchronous IO model and overlapped structure (http://blog.itpub.net/25897606/viewspace-705867/) data structure overlapped structure is mainly used for asynchronous i/ O operation whose data structure is defined as follows:

typedef struct _OVERLAPPED {
DWORD Internal; System reserved, the state of the system settings is stored
DWORD Internalhigh; System reserved, the length of the transmitted data is stored
DWORD Offset; Specifies the location of the file where the file location is the byte offset from the beginning of the file.
DWORD Offsethigh; Specifies the high word for the byte offset at which data is to be transferred
HANDLE hevent; Identifies an event and sets it to the signal state when the data transfer is complete
}overlapped;

The Overlapped I/O model can be implemented in several ways:

First, the Kernel object implementation
1. Treat the device handle as a synchronization object, ReadFile set the device handle to no signal. ReadFile the asynchronous I/O byte location must be specified in the overlapped structure.
2. Complete the I/O and set the information status to signaled.
3. Use WaitForSingleObject or waitformultipleobject to determine or invoke the GetOverlappedResult function from an asynchronous device.

Second, the event kernel object implementation
1. The overlapped member Heven identifies the event kernel object. CreateEvent, create an event for each request to initialize the Hevent member for each request. Call Waitformultipleobject to wait for one or all of them to complete.
2. The event object must be reset manually, using the auto-reset WaitForSingleObject () and the WaitForMultipleObjects () function will not be returned.

about auto-reset events and manual reset events
Auto-Reset events: WaitForSingleObject () and waitformultipleobjects () wait for the event to signal state and then automatically reset it to a non-signaled state, guaranteeing that only one of the threads waiting for this event will be awakened.
Manual reset Event: A call to ResetEvent () is required to reset the event. There may be several threads waiting for the same event, so that all waiting threads can run when the event becomes signaled. The SetEvent () function is used to set the event object to signal state, and resetevent () resets the event object to a non-signaled state, both of which require an event object handle as a parameter.


Third, asynchronous procedure call
After a overlapped I/O completes, the system calls the callback callback function. The system calls the callback function when the device handle is signaled, passing it the error code to complete the I/O request, the number of bytes transferred, and the address of the overlapped structure. The signal status can be set by the following five functions: Sleepex,waitforsingleobjectex,waitformultipleobjectex,singalobjectandwait, Msgwaitformultipleobjectsex.

Iv. Completion Port
The benefits of completing the port (I/O completion): No limit on the number of handle, thousands of connections can be processed. I/O completion port allows one thread to save a request temporarily, and another thread to do the actual service for it.
concurrency model and thread pool: In a typical concurrency model, the server creates a thread for each client, and if many customers request it at the same time, the threads are running, then the CPU is switched, the CPU spends more time on-line switching, and the thread does not get much CPU time. How many threads should be created in the end is appropriate, the Microsoft piece Help document should be said to be 2*cpu. Ideally, however, the best threads do not switch and can be reused like a thread pool. The I/O completion port is using a thread pool. A thread does not destroy the task at the end of its execution, but instead returns to the thread queue.

Asynchronous IO model and overlapped structure

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.