简体   繁体   中英

Calling boost asio io_service.run from multiple threads or one thread when queuing requests in handlers

When only queuing requests in handlers and not processing requests right away in the handlers, I'm thinking there's no benefit of calling io_service.run() from multiple threads.

In a Server class I invoke m_ios.run() from multiple threads:

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));
}

In a Service class managed by the server that handles async reads:

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();
    }
}

Is there any benefit of running io_service.run() from multiple threads in my case?

void first(){
// do your things
}
void second(){
// do your things
}
void allthings(){
    thread t1(first);
    thread t2(second);
    t1.join();
    t2.join();
}

int main() {

    allthings();

    return 0;
}

After a few benchmarks, its obvious that calling io_service.run() on one thread is just as fast as running io_service.run() on multiple threads in my example. Plus the multi-threaded one uses more memory.

So in case you don't handle anything in the read handlers themselves (actual request handling code that takes time to run), you won't get any benefit form using multiple threads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM