ASIO strand :: wrap don't need to serialize in order?

Am I getting lost on the difference between posting using strand :: wrap and strand :: post? Both seem to guarantee serialization, but how can you serialize with wrap and not get sequential order? Looks like they both had to do the same. When will I use one over the other?

Here is a bit more verbose pseudo code:

mystrand(ioservice); mystrand.post(myhandler1); mystrand.post(myhandler2); 

this ensures that my two handlers are serialized and executed in order even in the thread pool.

Now how is this different from the bottom?

  ioservice->post(mystrand.wrap(myhandler1)); ioservice->post(mystrand.wrap(myhandler2)); 

Looks like they are doing the same? Why use one over the other? I see that both are used, and I'm trying to figure out when one makes more sense than the other.

+4
source share
2 answers

wrap creates a callable object that, when called, will call dispatch chainwise. If you do not call the object returned by wrap , nothing will happen at all. So, calling the wrap result is like calling dispatch . How does this compare to post ? According to the documentation, post differs from dispatch in that it does not allow you to immediately call the passed function in the same context (stack stack) where post is called.

So wrap and post differ in two ways: their direct action and their ability to use the caller’s own context to perform this function.

I got all this by reading the documentation .

+1
source

This way

 mystrand(ioservice); mystrand.post(myhandler1); mystrand.post(myhandler2); 

myhandler1 is guaranteed to execute mystrand before myhandler2

but

 ioservice->post(mystrand.wrap(myhandler1)); ioservice->post(mystrand.wrap(myhandler2)); 

execution order is the execution order of wrapped handlers, which io_service :: post does not guarantee.

+1
source

Source: https://habr.com/ru/post/1385785/


All Articles