FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
August 03, 2007
Multithreaded Asynchronous I/O & I/O Completion Ports

Facilitating efficient handling of multiple asynchronous I/O requests

(Page 1 of 4)
Tom 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 ports—an 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:

  1. Create a new I/O completion port object.
  2. Associate one or more file descriptors with the port.
  3. Issue asynchronous read/write operations on the file descriptor(s).
  4. Retrieve completion notifications from the port and handle accordingly.

Multiple threads may monitor a single I/O completion port and retrieve completion events—the 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:

HANDLE CreateIoCompletionPort( HANDLE FileHandle, HANDLEExistingCompletionPort, ULONG_PTR CompletionKey, DWORD NumberOfConcurrentThreads );

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.

1 Multithreaded Asynchronous I/O | 2 Associating File Descriptors with a Port | 3 Retrieving Completed I/O Events from the Port | 4 A Practical Example: The Fire Web Server Next Page
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK