Device I/O overlapped

Source: Internet
Author: User

Overlapped, as its name implies, is overlapping. At first glance, it may be strange, overlap? Who and who overlap? Does it seem to have this concept in Win32 programming? To discuss this issue, we need to trace the access to device I/O in Win32. Users cannot directly access hardware as before, this layer is a "black box" for developers, but provides a set of corresponding API interfaces. developers are allowed to develop based on the provided interfaces, and lower-layer access is handed over to the driver or kernel. in Win32, the concept of a device is far beyond the range of moniter and printer. It may include files, directories, serial ports, parallel ports, pipelines, and the console. naturally, when we want to access this device, our first step is to open this device. The Win32 API provides createfile. For specific usage, see msdn, some of the parameters are used to determine whether the device already exists (dwcreationdisposition), whether to enable it in an exclusive mode, and so on. we may have come up with this idea: Ah, since we are dealing with devices, the device speed is so slow, and the CPU speed is so fast, how should we coordinate the two? For example, if I want to access the information on a floppy disk, even if it is read in a second, it is actually a great waste of CPU resources. yes, there is indeed this problem. Since there is a problem, we have to solve it. The Microsoft solution is here our discussion question: what is the meaning of the overlapped character? In fact, it means that when the program is waiting for device operation, it can continue to do so without blocking to that place and waiting for the device operation to return, this leads to the overlap between program running and device operation time. yes, it's amazing. How can the program know when the device operation is complete...
At this point, we need to introduce the concept of multithreading again. In fact, I believe everyone has a certain understanding of multithreading. In fact, multithreading is mainly
It is a synchronization problem. How can we coordinate the threads that "run without authorization"? What Win32 provides to us is waitforsingleobject.
And waitformultiobject, while Win32 provides a set of objects specifically for synchronization, including the critical section,
Mutex, semaphore, event, etc. Most of these objects belong to the kernel object.
The biggest difference between objects is that they are a Data Structure maintained by the system kernel. programs cannot directly access them. These objects
Both of them have two forms. Here we can call them signal-free and non-signal-free. In this way, when we use the wait function, we can
Whether the program is blocked in the wait area based on the signal. Simply put, when we call a function: waiforsingobject (event a);, if event A has a signal, then the program will run down. If there is no signal, the program will be blocked at the current position, waiting for it to become a signal. take an image of particles. For example, a thread is a car running on a highway. The wait function is used to open the car
At a crossroads, wait for the signal lights to be green. If so, the car will continue to run. Otherwise, sorry, please wait there.
The traffic signal turns green .. here, the most convenient thing for me to use is the event object, because we can easily operate on it, for example, setevent makes it a signal, the resetevent makes it non-signal-free. Of course, it is convenient for others, such as mutex, to make the program unable to be loaded repeatedly ..
In this way, when we want to access the device asynchronously (overlapped), we only need to first use the (overlapped) Flag during creatafile, and then read and write operations (corresponding to writefile and readfile) you can also use this sign...
Take a look at the following section:

/// Place 1 /// receives the specified character in an overlapping manner to check whether the function has been read successfully
Freadstat = readfile (hcom, lpblock, dwlength, & dwlength, & osread );
If (! Freadstat)
{
File: // The operations that overlap in the background...
If (getlasterror () = error_io_pending)
{
/// 2 places
File: // wait for 1 S. If the received event is in the signal state, the overlapping operation is completed. The timeout period is...
/// 3

If (waitforsingleobject (osread. hevent, 1000) = wait_timeout)
Dwlength = 0;
}
Else dwlength = 0; // exception
}

In this way, when the program reads data asynchronously, the program will immediately run down regardless of whether the device has completed the operation (
If it is synchronous, the thread will be blocked in this place ). in this way, we can do our own thing in the second place without having to worry about the device (This achieves the overlap of time ), it will not be used until we have to wait for three pieces of data to come in before further processing.
Wait for the device ..
This operation undoubtedly improves the efficiency and makes the coordination between the program and the device a lot. Of course, overlapped has its own new thread for processing, which is undoubtedly...

References: advanced windows
Mlutithreading applications in wim32

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.