简体   繁体   English

阻止Boost Asio Worker线程

[英]Blocking Boost Asio Worker Threads

I'm developing a network server based on Boost::Asio . 我正在开发基于Boost :: Asio的网络服务器。

I have a boost::thread_group of IO worker threads which I use to call boost::asio::io_service::run( ) 我有一个IO工作线程的boost::thread_groupboost::thread_group来调用boost::asio::io_service::run( )

When network activity occurs ASIO uses one of these worker threads to process the activity (eg. Accept or Receive). 当发生网络活动时, ASIO使用这些工作线程之一来处理活动(例如,接受或接收)。

My application then does some work, possibly some calculation, possibly some other IO (via boost) and possibly some database activity. 然后,我的应用程序会做一些工作,可能会进行一些计算,可能会进行其他一些IO(通过boost),可能还会进行一些数据库活动。

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? 在IO线程上执行(可能需要大量工作)是否会使io_service感到悲伤?

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? 在IO线程上执行(可能需要大量工作)是否会使io_service感到悲伤?

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 . 在由调用的处理程序执行长时间运行的操作io_service可以从由被调用阻止其他处理程序io_service Consider the simplest example with a single thread invoking io_service::run() . 考虑最简单的示例,该示例具有一个调用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 . 如果处理程序由异步操作调用,例如async_read()然后执行一些可能长时间运行的数据库操作,则在长时间运行的操作完成并且处理程序将控制权返回给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. 通常可以通过从多个线程调用io_service::run()并使用strand来确保对使用共享数据的处理程序的独占访问来缓解这种情况。 Though if all of your handlers perform some long running operations, you might need to investigate alternative designs. 尽管如果所有处理程序都执行一些长时间运行的操作,则可能需要研究替代设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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