【发布时间】:2020-02-21 13:34:37
【问题描述】:
当仅在处理程序中排队请求而不立即在处理程序中处理请求时,我认为从多个线程调用 io_service.run() 没有任何好处。
在 Server 类中,我从多个线程调用 m_ios.run():
const unsigned int threadPoolSize = std::thread::hardware_concurrency() * 2;
for (unsigned int i = 0; i < threadPoolSize; i++)
{
std::unique_ptr<std::thread> th(new std::thread([this]() { m_ios.run(); }));
m_threadPool.push_back(std::move(th));
}
在由处理异步读取的服务器管理的 Service 类中:
void handleNextRequest()
{
m_connection->async_read(m_request, m_connection->getStrand().wrap(boost::bind(&Service::onRequestRecieved, this, boost::asio::placeholders::error)));
}
void onRequestRecieved(const boost::system::error_code& ec)
{
if (!ec)
{
addServerRequest(m_request); // Adds request to a thread-safe queue
handleNextRequest();
}
else
{
stop();
}
}
在我的情况下,从多个线程运行 io_service.run() 有什么好处吗?
【问题讨论】:
标签: c++ multithreading boost boost-asio