简体   繁体   中英

Blocking Boost Asio Worker Threads

I'm developing a network server based on Boost::Asio .

I have a boost::thread_group of IO worker threads which I use to call boost::asio::io_service::run( )

When network activity occurs ASIO uses one of these worker threads to process the activity (eg. Accept or Receive).

My application then does some work, possibly some calculation, possibly some other IO (via boost) and possibly some database activity.

I'd like to know what the implications are of doing said work within these threads. Specifically:

  • Does carrying out ( possibly significant work ) on the IO threads cause the io_service any grief?

And less specifically: any other issues I should be thinking about.

Does carrying out ( possibly significant work ) on the IO threads cause the io_service any grief?

It really depends what you mean by grief. Performing long running operations in a handler invoked by an io_service can block additional handlers from being invoked by the io_service . Consider the simplest example with a single thread invoking io_service::run() . If the handler invoked by an asynchronous operation, say async_read() then performs some database operations that could be long running, additional outstanding asynchronous operations will not have their handlers invoked until the long running operation is complete and the handler returns control to the io_service .

This is typically mitigated by invoking io_service::run() from multiple threads, and using a strand to ensure exclusive access to handlers that used shared data. Though if all of your handlers perform some long running operations, you might need to investigate alternative designs.

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