0

When do I have to use io_service::strand?

From the documentation it seems that I need it when I want to prevent several handlers to be executed concurrently:

The io_service::strand class provides the ability to post and dispatch handlers with the guarantee that none of those handlers will execute concurrently

As far as I know, this can happen if I have several threads calling run on the same io_service object.

So, from this point of view, it seems that the only reasonable way to use strand is when we have some shared memory inside our handlers passed to the io_service::post function and we don't want to use any synchronization primitives like boost::mutex inside them for some reason. Am I right?

Or is there any internal io_service problems with having two threads calling io_service::run concurrently?

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
  • Maybe [this](http://stackoverflow.com/questions/15875359/correct-usage-of-asioio-servicestrand) helps. – mkaes Aug 03 '16 at 08:43
  • @mkaes Well, that question is about specific `boost::asio` functions like `async_read` and `async_write`. My question is about more general stuff related to the `io_service` – FrozenHeart Aug 03 '16 at 08:47
  • 1
    Maybe this is [SO answer](http://stackoverflow.com/questions/12794107/why-do-i-need-strand-per-connection-when-using-boostasio/12801042#12801042) is more relevant? – aichao Aug 03 '16 at 11:36
  • FrozenHeart: the docs are very clear that most (non-special) operations on io_service are threadsafe. http://www.boost.org/doc/libs/1_61_0/doc/html/boost_asio/reference/io_service.html#boost_asio.reference.io_service.thread_safety – sehe Aug 04 '16 at 09:04

0 Answers0