
Reading and writing in Win32 is significantly different from reading and writing serial communications
ports in 16-bit Windows. 16-bit Windows only has the ReadComm and WriteComm functions. Win32
reading and writing can involve many more functions and choices. These issues are discussed below.
Nonoverlapped I/O
Nonoverlapped I/O is very straightforward, though it has limitations. An operation takes place while the
calling thread is blocked. Once the operation is complete, the function returns and the thread can
continue its work. This type of I/O is useful for multithreaded applications because while one thread is
blocked on an I/O operation, other threads can still perform work. It is the responsibility of the
application to serialize access to the port correctly. If one thread is blocked waiting for its I/O operation
to complete, all other threads that subsequently call a communications API will be blocked until the
original operation completes. For instance, if one thread were waiting for a ReadFile function to return,
any other thread that issued a WriteFile function would be blocked.
One of the many factors to consider when choosing between nonoverlapped and overlapped operations
is portability. Overlapped operation is not a good choice because most operating systems do not
support it. Most operating systems support some form of multithreading, however, so multithreaded
nonoverlapped I/O may be the best choice for portability reasons.
Overlapped I/O
Overlapped I/O is not as straightforward as nonoverlapped I/O, but allows more flexibility and
efficiency. A port open for overlapped operations allows multiple threads to do I/O operations at the
same time and perform other work while the operations are pending. Furthermore, the behavior of
overlapped operations allows a single thread to issue many different requests and do work in the
background while the operations are pending.
In both single-threaded and multithreaded applications, some synchronization must take place between
issuing requests and processing the results. One thread will have to be blocked until the result of an
operation is available. The advantage is that overlapped I/O allows a thread to do some work between
the time of the request and its completion. If no work can be done, then the only case for overlapped
I/O is that it allows for better user responsiveness.
Overlapped I/O is the type of operation that the MTTTY sample uses. It creates a thread that is
responsible for reading the port's data and reading the port's status. It also performs periodic
background work. The program creates another thread exclusively for writing data out the port.
Note Applications sometimes abuse multithreading systems by creating too many
threads. Although using multiple threads can resolve many difficult problems, creating
excessive threads is not the most efficient use of them in an application. Threads are less
a strain on the system than processes but still require system resources such as CPU
time and memory. An application that creates excessive threads may adversely affect the
performance of the entire system. A better use of threads is to create a different request
queue for each type of job and to have a worker thread issue an I/O request for each
entry in the request queue. This method is used by the MTTTY sample provided with this
article.
An overlapped I/O operation has two parts: the creation of the operation and the detection of its
completion. Creating the operation entails setting up an OVERLAPPED structure, creating a manual-
reset event for synchronization, and calling the appropriate function (ReadFile or WriteFile). The I/O
operation may or may not be completed immediately. It is an error for an application to assume that a
request for an overlapped operation always yields an overlapped operation. If an operation is
completed immediately, an application needs to be ready to continue processing normally. The second
part of an overlapped operation is to detect its completion. Detecting completion of the operation
involves waiting for the event handle, checking the overlapped result, and then handling the data. The
reason that there is more work involved with an overlapped operation is that there are more points of
failure. If a nonoverlapped operation fails, the function just returns an error-return result. If an
overlapped operation fails, it can fail in the creation of the operation or while the operation is pending.
You may also have a time-out of the operation or a time-out waiting for the signal that the operation is
complete.
Reading
The ReadFile function issues a read operation. ReadFileEx also issues a read operation, but since it is
页码,3
24(W)w
2010/7/4http://msdn.microsoft.com/en-us/librar
/ms810467(printer).aspx
- 1
- 2
- 3
前往页