I/O 完成端口的优缺点 [英] I/O completion port's advantages and disadvantages

查看:36
本文介绍了I/O 完成端口的优缺点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么很多人说 I/O 完成端口是快速和漂亮的模型?
I/O 完成端口的优缺点是什么?

Why do many people say I/O completion port is fast and nice model?
What is the I/O completion port's advantages and disadvantages?

我想知道一些比其他模型更快的 IOCP.

I want to know some points which make faster IOCP than other model.

如果能和其他模型(select、epoll、传统的多线程/进程)进行对比说明就更好了.

If you can explain it comparing other models(select, epoll, traditional multi thread/process), it would be better.

推荐答案

I/O 完成端口很棒.没有更好的词来形容它们.如果 Windows 中的任何事情都做得对,那就是完成端口.

I/O completion ports are awesome. There's no better word to describe them. If anything in Windows was done right, it's completion ports.

您可以创建一定数量的线程(实际上并不重要)并使它们全部阻塞在一个完成端口上,直到发生事件(您手动发布的一个事件,或者来自 定时器 异步 I/O 或其他)到达.然后完成端口将唤醒一个线程来处理事件,直到您指定的限制.如果你没有指定任何东西,它会假设最多 CPU 核心数",这真的很好.

You can create some number of threads (does not really matter how many) and make them all block on one completion port until an event (either one you post manually, or an event from a timer or asynchronous I/O, or whatever) arrives. Then the completion port will wake one thread to handle the event, up to the limit that you specified. If you didn't specify anything, it will assume "up to number of CPU cores", which is really nice.

如果已经有超过最大限制的活跃线程数,它将等待其中一个线程完成,然后在线程进入等待状态时立即将事件交给线程.此外,它总是会以 LIFO 顺序唤醒线程,因此缓存可能仍然是热的.

If there are already more threads active than the maximum limit, it will wait until one of them is done and then hand the event to the thread as soon as it goes to wait state. Also, it will always wake threads in a LIFO order, so chances are that caches are still warm.

换句话说,完成端口是一种无需大惊小怪的事件轮询"以及尽可能多地填充 CPU"的解决方案.

In other words, completion ports are a no-fuss "poll for events" as well as "fill CPU as much as you can" solution.

您可以在完成端口、套接字或其他任何可等待的地方抛出文件读取和写入.而且,您可以根据需要发布自己的活动.每个自定义事件至少有一个整数和一个数据指针(如果您使用默认结构),但您并不仅限于此,因为系统也很乐意接受任何其他结构.

You can throw file reads and writes at a completion port, sockets, or anything else that's waitable. And, you can post your own events if you want. Each custom event has at least one integer and one pointer worth of data (if you use the default structure), but you are not really limited to that as the system will happily accept any other structure too.

此外,完成端口很快,真的非常快.曾几何时,我需要从另一个线程通知一个线程.碰巧的是,该线程已经有一个用于文件 I/O 的完成端口,但它没有泵送消息.所以,我想知道我是否应该咬紧牙关,为了简单起见使用完成端口,即使发布线程消息显然会更有效率.我犹豫不决,所以我进行了基准测试.令人惊讶的是,事实证明完成端口的速度提高了大约 3 倍.所以……更快更灵活,这个决定并不难.

Also, completion ports are fast, really really fast. Once upon a time, I needed to notify one thread from another. As it happened, that thread already had a completion port for file I/O, but it didn't pump messages. So, I wondered if I should just bite the bullet and use the completion port for simplicity, even though posting a thread message would obviously be much more efficient. I was undecided, so I benchmarked. Surprise, it turned out completion ports were about 3 times faster. So... faster and more flexible, the decision was not hard.

这篇关于I/O 完成端口的优缺点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆