Reputation: 295
I'm lost on the distinction between posting using strand::wrap and a strand::post? Seems like both guarantee serialization yet how can you serialize with wrap and not get consistent order? Seems like they both would have to do the same thing. When would I use one over the other?
Here is a little more detail pseudo code:
mystrand(ioservice);
mystrand.post(myhandler1);
mystrand.post(myhandler2);
this guarantees my two handlers are serialized and executed in order even in a thread pool.
Now, how is that different from below?
ioservice->post(mystrand.wrap(myhandler1));
ioservice->post(mystrand.wrap(myhandler2));
Seems like they do the same thing? Why use one over the other? I see both used and am trying to figure out when one makes more sense than the other.
Upvotes: 5
Views: 1287
Reputation: 136
This way
mystrand(ioservice);
mystrand.post(myhandler1);
mystrand.post(myhandler2);
myhandler1 is guaranteed by mystrand to be executed before myhandler2
but
ioservice->post(mystrand.wrap(myhandler1));
ioservice->post(mystrand.wrap(myhandler2));
the execution order is the order of executing wrapped handlers, which io_service::post does not guarantee.
Upvotes: 3
Reputation: 249404
wrap
creates a callable object which, when called, will call dispatch
on a strand. If you don't call the object returned by wrap
, nothing much will happen at all. So, calling the result of wrap
is like calling dispatch
. Now how does that compare to post
? According to the documentation, post
differs from dispatch
in that it does not allow the passed function to be invoked right away, within the same context (stack frame) where post
is called.
So wrap
and post
differ in two ways: the immediacy of their action, and their ability to use the caller's own context to execute the given function.
I got all this by reading the documentation.
Upvotes: 2