如何提高运行在多个线程ASIO解析服务? [英] How to run boost asio resolver service on more threads?

查看:149
本文介绍了如何提高运行在多个线程ASIO解析服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用boost ::支持ASIO ::知识产权:: UDP ::解析器在SNMPV2实现,以确定wheather主机是否可达与否。

I am using boost::asio::ip::udp::resolver in an SNMPV2 implementation to determine wheather a host is reachable or not.

using Resolver = boost::asio::ip::udp::resolver;
Resolver resolver(ioService);
Resolver::query query(connectOptions.getHost(),
        connectOptions.getPort());
Resolver::iterator endpointIterator;
BOOST_LOG_SEV(logger, Severity::debug) << "Waiting for async resolve";
endpointIterator = resolver.async_resolve(query, yield);
BOOST_LOG_SEV(logger, Severity::debug) << "Async resolve done";
if (endpointIterator == Resolver::iterator{}) { // unreachable host
    using namespace boost::system;
    throw system_error{error_code{SnmpWrapperError::BadHostname}};
}

我有一个测试情况下我测试发生了什么事时,一个不存在的主机名和主机exitent查询paralell:

I have one test case where I test what is happening when a non existent hostname and an exitent hostname is queried paralell:

2013-09-16 10:45:28.687001: [DEBUG   ] 0x88baf8 SnmpConnection: connect                                                                                              
2013-09-16 10:45:28.687396: [DEBUG   ] 0x88baf8 SnmpConnection: host: non_existent_host_name_                                                                        
2013-09-16 10:45:28.687434: [DEBUG   ] 0x88baf8 SnmpConnection: port: 1611                                                                                           
2013-09-16 10:45:28.687456: [DEBUG   ] 0x88baf8 SnmpConnection: Waiting for async resolve                                                                            
2013-09-16 10:45:28.687675: [DEBUG   ] 0x88c608 SnmpConnection: connect                                                                                              
2013-09-16 10:45:28.687853: [DEBUG   ] 0x88c608 SnmpConnection: host: 127.0.0.1                                                                                      
2013-09-16 10:45:28.687883: [DEBUG   ] 0x88c608 SnmpConnection: port: 1611                                                                                           
2013-09-16 10:45:28.687904: [DEBUG   ] 0x88c608 SnmpConnection: Waiting for async resolve                                                                            
2013-09-16 10:45:31.113527: [ERROR   ] 0x88baf8 SnmpConnection: Host not found (authoritative)                                                                       
2013-09-16 10:45:31.113708: [DEBUG   ] 0x88c608 SnmpConnection: Async resolve done                                                                                   
2013-09-16 10:45:31.113738: [DEBUG   ] 0x88c608 SnmpConnection: Connecting to 127.0.0.1:1611
...

从它可以看出,与可达地址的物体被阻塞,直到另一个的决心与错误(3秒)完成的日志。
我的假设是,短耳解析服务使用一个线程,因此相对待主机不可达的一个查询可能会阻止即将到来的决心请求的处理。

From the logs it can be seen that the object with the reachable address is blocked until the other one's resolve is finished with the error (3 seconds). My assumption is that Asio resolver service uses one thread, so one query towards one unreachable host might block the processing of upcoming resolve requests.

解决方法是在多个线程运行解析服务,这可能吗?或者,才有可能有一个解析服务,对插座的工作方式与UDP服务支持(而不是使用::的getaddrinfo)?

Workaround would be to run the resolver service on more threads, is that possible? Or would it be possible to have a resolver service which works on sockets like the udp service does (instead of using ::getaddrinfo)?

推荐答案

您需要的是两个 io_service对象的I​​OService ,因为每一个都是由一个线程运行。我的意思是,你通过调用 io_service对象::运行阻塞线程的正常执行。

What you need are two io_service ioService, because each one is run by a thread. By that I mean that you block the normal execution of a thread by calling io_service::run.

我认为,code本身是正确的。

I think that the code itself is correct.

这篇关于如何提高运行在多个线程ASIO解析服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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