为什么在Boost.ASIO中我们需要许多接受器? [英] Why would we need many acceptors in the Boost.ASIO?

查看:53
本文介绍了为什么在Boost.ASIO中我们需要许多接受器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,我们可以在boost :: asio中使用多个接受器.

As known, we can use multiple acceptors in boost::asio.

boost::asio::io_service io_service_acceptors;
std::vector<boost::thread> thr_grp_acceptors;
unsigned int thread_num_acceptors = 2;

for(size_t i = 0; i < thread_num_acceptors; ++i) {
    thr_grp_acceptors.emplace_back(
        boost::bind(&boost::asio::io_service::run, &io_service_acceptors));

但是io_service_acceptors大于1有什么意义吗?

But is there any sense in doing io_service_acceptors more than 1?

  1. Boost.ASIO使用最佳的无阻塞解复用机制(epoll,IOCP等).

  1. Boost.ASIO uses optimal non-blocking demultiplexing mechanism (epoll, IOCP, ...).

即使在epoll之后和accept之前都会发生网络错误,也不会阻止接受,因为我们可以设置non_blocking(true);:

Also even if network error will occur after epoll and before accept then accept will not been blocked, because we can set non_blocking(true);: Boost asio non-blocking IO without callbacks

http://man7.org/linux/man-pages/man2/accept.2.html

在SIGIO之后,可能并不总是有连接在等待 Delivery或select(2),poll(2)或epoll(7)返回可读性 事件,因为连接可能已被连接删除 异步网络错误或accept()之前的另一个线程 叫.如果发生这种情况,则呼叫将阻止等待 下一个连接到达. 为确保accept()永远不会阻塞, 传递的套接字sockfd需要设置O_NONBLOCK 标志(请参见 socket(7)).

There may not always be a connection waiting after a SIGIO is delivered or select(2), poll(2), or epoll(7) return a readability event because the connection might have been removed by an asynchronous network error or another thread before accept() is called. If this happens, then the call will block waiting for the next connection to arrive. To ensure that accept() never blocks, the passed socket sockfd needs to have the O_NONBLOCK flag set (see socket(7)).

  1. 接受器始终工作迅速(仅接受连接,创建新的套接字,并将其传递给线程安全的队列以在其他线程上处理它们-以便通过这些连接进行数据交换).

因此,如果接受者永不阻塞并且接受者总是快速工作,那么一个CPU内核上的一个接受者可以处理所有新连接吗?

So if acceptor never blocks and the acceptor always works quickly so can one acceptor on the one CPU-Core process all the new connections?

如果可以的话,那为什么我们需要许多接受器?

And if they can, then why would we need many aceptors?

推荐答案

受体绑定到特定端点.

此外,协议选择方面也有所不同.

What's more it differs with respect to protocol choice.

因此,您可以在多个协议的多个端点上具有多个接受器.

So, you could have several acceptors for several endpoints on several protocols.

事实上,您似乎希望可以在单个io_service上全部运行它们,而无需在多个线程上运行它.

What you seem to be after is, indeed, you can run them all on a single io_service and there would not be any need to run it on more than one thread.

这篇关于为什么在Boost.ASIO中我们需要许多接受器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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