I’m experimenting with Boost.Asio strand for a server I’m writing and wanted to clarify a few things.
Let’s assume we have SomeClass with something like this:
void SomeClass::foo()
{
strand_.dispatch(boost::bind(&SomeClass::bar, this));
}
Further, the strand has an io_service with multiple threads calling run().
In the documentation on strand::dispatch() I read it guarantees that handlers posted or dispatched through the strand will not execute concurrently and if this is met the handler may execute in this function. What decides if the handler gets to be executed immediately?
In this case with multiple threads, will dispatch block if multiple io_service threads call SomeClass::foo concurrently? I.e. block for all but the one that gets executed. If so, is the handlers executed in order (the order they called dispatch())?
Logically, it would be impossible for this guarantee to be met without some possibility of (b)locking. The code for
dispatch(in detail/impl/strand_service.hpp from 1.46.1) confirms this. That’s the nice thing about Boost, it’s all there for you to see. There is info on order of handler invocation in the docs here, including a comment that sometimes no ordering guarantee is provided.