What's exactly the difference between using strand::post and io_service::post with strand::wrap?
In my understanding, posting handlers to a strand
object means:
- Only one of the posted handlers is executed at a time.
- The handlers are invoked in order.
Posting handlers directly to an io_service
object and wrapping them by strand::wrap
also means that only one of the posted 开发者_JAVA技巧handlers is executed at a time, but not in order.
Is there any other difference? And how can I run two (or more) different kind of works (so, different handlers/functions) parallel (in different threads) by using strand
?
If you want them to run in parallel don't use stands. Strands are for serializing. Just post to the service and have the service run in a thread pool.
But you bring up a good point that I would like someone to answer. What exactly is the difference? If wrap serializes all handlers then how could they get out of order, i.e. seems like it does the same thing as posting through a strand? Where would you use one over the other?
In fact, strand
is a queue, so posting a handler to io_service
by wrap
is same as not to wrap because each time you post it, you do in a distinct temp strand
(each queue contains only one handler -- all those handlers can be executed concurrently because they are not in the same strand
). If you need serialized handlers, they must be wrapped by the same strand
object (which therefore contains more than one handler).
精彩评论