Windows 上最好的 epoll/kqueue/select equivalient 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/67082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best epoll/kqueue/select equvalient on Windows?
提问by blackwing
What is Windows' best I/O event notification facility?
Windows 最好的 I/O 事件通知工具是什么?
By best I mean something that ...
我最好的意思是......
- doesn't have a limit on number of input file descriptors
- works on all file descriptors (disk files, sockets, ...)
- provides various notification modes (edge triggered, limit triggered)
- 对输入文件描述符的数量没有限制
- 适用于所有文件描述符(磁盘文件、套接字等)
- 提供多种通知方式(边缘触发、限位触发)
回答by Chris Smith
In Windows, async operations are done by file operation, not by descriptor. There are several ways to wait on file operations to complete asynchronously.
在 Windows 中,异步操作由文件操作完成,而不是由描述符完成。有几种方法可以等待文件操作异步完成。
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.
在 Win32 中,异步操作使用该OVERLAPPED
结构来包含有关未完成 IO 操作的状态。
- Associate the files with an IO Completion Portand 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
- 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 onMAXIMUM_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) - 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 functionupon completion each.
- Use
ReadFileEx
andWriteFileEx
to queue Asynchronous Procedure Calls(APCs) to the calling thread andSleepEx
(orWaitFor{Single|Multiple}ObjectsEx
) withAlertable 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.
- 将文件与IO 完成端口关联并分派异步 IO 请求。当操作完成时,它将在队列中放置一条完成消息,您的工作线程可以等待并在它们到达时检索。您还可以将用户定义的消息放入队列中。完成端口可以使用的文件或排队消息的数量没有限制
- 使用事件调度每个 IO 操作。与操作关联的事件将在完成时发出信号(满足等待)。用于
WaitForMultipleObjects
一次等待所有事件。这样做的缺点是只能MAXIMUM_WAIT_OBJECTS
一次等待对象 (64)。您还可以同时等待其他类型的事件(进程/线程终止、互斥、事件、信号量) - 使用线程池。线程池可以接受无限数量的对象和文件操作来等待并在每个完成后执行用户定义的函数。
- 使用
ReadFileEx
和WriteFileEx
将异步过程调用(APC)排队到调用线程和SleepEx
(或WaitFor{Single|Multiple}ObjectsEx
)Alertable TRUE
用于在每个操作完成时接收通知消息。这种方法类似于IO完成端口,但只对一个线程有效。
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.
Windows NT 内核在内部不区分套接字、磁盘文件、管道等文件操作:所有这些选项都适用于所有文件类型。
回答by schlamar
libuv
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.
libuv
为 Unix 和 Windows 提供事件 I/O,并支持套接字、文件和管道。它是 Node.js 的平台层。
More details are at: http://nikhilm.github.io/uvbook/introduction.html
回答by Heron
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.
据我所知,目前还没有。我和一个朋友正在研究开源 Windows epoll 实现(下面的链接),但我们在弄清楚如何使其与 Linux 实现相同的问题上遇到了问题。
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.
- 在 Linux 中,文件描述符和套接字描述符是可以互换的,但在 Windows 中它们不是。两者都必须与 epoll 实现兼容。
- 在 Windows 中,获取内核事件非常棘手……这就是 epoll 在 Linux 中的工作方式。我们猜测使用我们的跨平台 epoll 库的程序在 Windows 中的运行速度会明显慢于 Linux。
I'll try to come back and update this post as we make progress with the project.
随着项目的进展,我会尝试回来更新这篇文章。
回答by Nouil
select() function is POSIX and usable on windows including "winsock.h" or "winsock2.h".
select() 函数是 POSIX,可用于 Windows,包括“winsock.h”或“winsock2.h”。