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

4

45

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)
Valeryvalerye answered 15/9, 2008 at 21:20 Comment(0)
S
46

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.

Swivel answered 15/9, 2008 at 22:23 Comment(3)
MAXIMUM_WAIT_OBJECTS is 64 also on XP and later.Decury
I recommend the 4th option (APCs): Is has no limitation on the number of connections and you don't need to fill any weird arrays like select et al. Just Schedule your async IO using WriteFileEx/ReadFileEx and use SleepEx/WaitFor{Single|Multiple}ObjectsEx to set the thread to an alertable state.Bolitho
You can refer to this code snippet and article mentioned there gist.github.com/abdul-sami/23e1321c550dc94a9558Zoltai
S
6

libuv

libuv offers evented I/O for Unix and Windows and has support for socket, files and pipes. It is the platform layer of Node.js.

More details are at: http://nikhilm.github.io/uvbook/introduction.html

Sandarac answered 16/4, 2013 at 10:26 Comment(0)
L
2

There isn't one yet, as far as I am aware. A friend and I are working on an open source Windows epoll implementation (link below) but we're running into issues figuring out how to make it act the same as the Linux implementation.

Current obstacles:

  • In Linux, file descriptors and socket descriptors are interchangeable, but in Windows they are not. Both must be compatible with an epoll implementation.
  • In Windows it's quite tricky to get kernel events... which is how epoll works in Linux. We're guessing that a program using our cross-platform epoll library will run noticeably slower in Windows than Linux.

I'll try to come back and update this post as we make progress with the project.

http://sourceforge.net/projects/cpoll

Lajoie answered 15/9, 2008 at 22:13 Comment(2)
From the project page for cpoll: "As of 2009-11-12 0:00:00 GMT, this project is no longer under active development."Floorman
Such a shame, I really wish this was possible on Windows :-(Kellene
F
0

select() function is POSIX and usable on windows including "winsock.h" or "winsock2.h".

Filamentary answered 2/8, 2012 at 10:30 Comment(1)
But not for files, so it fails criterion 2.Vertebra

© 2022 - 2024 — McMap. All rights reserved.