What is the best epoll/kqueue/select equvalient on Windows?

blackwing picture blackwing · Sep 15, 2008 · Viewed 29.2k times · Source

What is Windows' best I/O event notification facility?

By best I mean something that ...

  1. doesn't have a limit on number of input file descriptors
  2. works on all file descriptors (disk files, sockets, ...)
  3. provides various notification modes (edge triggered, limit triggered)

Answer

Chris Smith picture Chris Smith · Sep 16, 2008

In Windows, async operations are done by file operation, not by descriptor. There are several ways to wait on file operations to complete asynchronously.

For example, if you want to know when data is available on a network socket, issue an async read request on the socket and when it completes, the data was available and was retrieved.

In Win32, async operations use the OVERLAPPED structure to contain state about an outstanding IO operation.

  1. Associate the files with an IO Completion Port and dispatch async IO requests. When an operation completes, it will put a completion message on the queue which your worker thread(s) can wait on and retrieve as they arrive. You can also put user defined messages into the queue. There is no limit to how many files or queued messages can be used with a completion port
  2. Dispatch each IO operation with an event. The event associated with an operation will become signaled (satisfy a wait) when it completes. Use WaitForMultipleObjects to wait on all the events at once. This has the disadvantage of only being able to wait on MAXIMUM_WAIT_OBJECTS objects at once (64). You can also wait on other types of events at the same time (process/thread termination, mutexes, events, semaphores)
  3. Use a thread pool. The thread pool can take an unlimited number of objects and file operations to wait on and execute a user defined function upon completion each.
  4. Use ReadFileEx and WriteFileEx to queue Asynchronous Procedure Calls (APCs) to the calling thread and SleepEx (or WaitFor{Single|Multiple}ObjectsEx) with Alertable TRUE to receive a notification message for each operation when it completes. This method is similar to an IO completion port, but only works for one thread.

The Windows NT kernel makes no distinction between socket, disk file, pipe, etc. file operations internally: all of these options will work with all the file types.