August 03, 2007
Multithreaded Asynchronous I/O & I/O Completion PortsFacilitating efficient handling of multiple asynchronous I/O requestsTom R. Dial
I/O completion ports provide an elegant solution to the problem of writing scalable server applications that use multithreading and asynchronous I/O.
Tom is a development team leader for Hyland Software. He can be contacted at tdial@kavaga.com.
When developing server applications, it is important to consider scalability, which usually boils down to two issues. First, work must be distributed across threads or processes to take advantage of today's multiprocessor hosts. Second, I/O operations must be scheduled efficiently to maximize responsiveness and throughput. In this article, I examine I/O completion portsan elegant innovation available on Windows that helps you accomplish both of these goals.
I/O completion ports provide a mechanism that facilitates efficient handling of multiple asynchronous I/O requests in a program. The basic steps for using them are:
Multiple threads may monitor a single I/O completion port and retrieve completion eventsthe operating system effectively manages the thread pool, ensuring that the completion events are distributed efficiently across threads in the pool.
A new I/O completion port is created with the CreateIoCompletionPort API. The same function, when called in a slightly different way, is used to associate file descriptors with an existing completion port. The prototype for the function looks like this:
When creating a new port object, the caller simply passes INVALID_HANDLE_VALUE for the first parameter, NULL for the second and third parameters, and either zero or a positive number for the ConcurrentThreads parameter. The last parameter specifies the maximum number of threads Windows schedules to concurrently process I/O completion events. Passing zero tells the operating system to allow at least as many threads as processors, which is a reasonable default. For a discussion of why you might want to schedule more threads than available processors, see Programming Server-Side Applications for Windows 2000 by Jeffrey Richter and Jason D. Clark.
|
|
||||||||||||||||||||||||||||||
|
|
|
|