本文介绍了为什么在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

  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中我们需要许多接受器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 23:47