Device I/O OVERLAPPED (zz)

Source: Internet
Author: User
Tags readfile
Device I/O OVERLAPPED

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 it back.
Access to device I/O
In WIN32, users cannot directly access the hardware as before, making this layer
It is a "black box" for developers, but provides a set of corresponding API interfaces, allowing developers
Developed based on the provided interface, and the 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 can include files, directories, serial ports, parallel ports, pipelines, and the console. Naturally, when we want
When accessing this device, our first step is to open this device, where WIN32 API
CreateFile is provided. For more information, see MSDN.
The parameter table shows whether the specified device already exists (dwCreationDisposition ),
Whether to enable it in an exclusive mode.
You may have come up with this idea,
So the device speed is so slow, and the CPU speed is so fast, what should the two do?
What about coordination? For example, I want to access the information on a floppy disk, even if it is a second.
After reading it, it is actually a great waste of CPU. Yes, indeed
This problem occurs. Since there is a problem, we have to solve it, while MicroSoft
Solution: OverLapped
What exactly does this character mean? In fact, it means that when the program is waiting
When the device is operating, it can continue to do so without blocking to that place to wait
Device Operation Return, resulting in the program running and device operation time
Overlap. Yes, it's like this. How can the program know how the device operates?
When is the result returned...
At this point, we need to introduce the concept of multithreading.
I believe everyone has a certain understanding of multithreading. In fact, multithreading is mainly
It's just a matter of synchronization. How can we coordinate these issues"
The thread that Win32 provides to us is waitforsingleobject.
And waitformultiobject, while Win32 provides
A set of objects specifically used for synchronization include the critical section,
Mutex, semaphore, event, and so on, most of which are
Is a kernel object.
The biggest difference between objects is that they belong to system kernel maintenance.
The program cannot directly access them. These objects
They both have two forms. Here we can call it a signal and
No signal. 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 );
If event A has a signal, the program will run down.
If there is no signal, the program will be blocked at the current position and wait until it changes
There is a signal to give an image of the particle, such as the thread is in the highway
The wait function is used to drive the car
At a crossroads, wait for the signal lights to be green. If
Yes. The car then runs down. Otherwise, I'm sorry. Please wait there.
The traffic signal turns green ..
Here, the most convenient thing to use is the Event object,
Because we can easily operate on it, such as SetEvent
And ResetEvent makes it signal-free,
Of course, some others, such as Mutex, make the program unique.
Loading and other places are also very convenient ..
In this way, when we want to access the device asynchronously (OVERLAPPED)
When you create a file, you only need to use it (OVERLAPPED) first)
Mark, 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...
/// If (WaitForSingleObject (osRead. hEvent, 1000) = WAIT_TIMEOUT)
DwLength = 0;
}
Else dwLength = 0; // exception
}

In this way, when the program reads data asynchronously
If the operation on the device is complete, the program will immediately run down without waiting for it to return (
If it is synchronous, the thread will be blocked in this place). In this way, we
In the second place, you can do your own thing without worrying about the device (this way
This achieves Time overlap) until we need to wait for three pieces of information.
We will only use
Wait for the device ..
Such operations undoubtedly improve the efficiency, making programs and devices
Of course, the OVERLAPPED contains itself.
There is no doubt that the thread is newly opened for processing...

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.