Overlapped I/O: some functions are still missing
One of the things that I really like in Windows API is overlapped I/O. I've written dozens of network servers using overlapped I/O (for both sockets and files) with only a limited number of threads. I'm also a driver writer, so that I know well how overlapped I/O is implemented "behind the scenes".
The only thing that always bothered me is that some API functions don't support overlapped mode. For instance, creating a file (i.e. calling CreateFile
) always works synchronously. This is pity, because those methods could support overlapped (asynchronous) mode as 开发者_如何学编程well. For instance, when a file is created (or opened) - the file system driver receives an IRP_MJ_CREATE
request, for which it may (and usually does) return STATUS_PENDING
.
My question is: is there an option to open the file asynchronously nevertheless? (but please don't tell me to create another thread to open the file).
I suspect that no asynchronous version exists because CreateFile
is fundamentally a blocking operation: http://www.osronline.com/article.cfm?article=484 (scroll down to Creates Are Even More Cancellable):
Creates Are Even More Cancellable
As you can imagine, creates are a bit of a special operation within the O/S. One particularly interesting point is that they are always handled synchronously within the I/O Manager, so there is no way to send an asynchronous create operation. Also, in previous versions of the O/S, the I/O Manager performed a non-alertable kernel wait if the create request was pended from within a driver. Therefore, even terminating the thread was not enough to cancel a create request that the user felt was taking too long.
Sorry, but there is no way to open a file asynchronously without using a separate thread.
The problem with I/O completion ports is that (a) you have too little freedom and (b) despite being in the kernel, it is far from being optimal. TrustLeap has made a lot of noise about these performance issues recently, when comparing Windows to Linux.
Too sad that Microsoft does not follow-up (TrustLeap said Bill Stapples claimed that IIS users did not want performances and rather wanted more productivity).
I guess that these positions define the platforms.
精彩评论